欢迎您光临自学哈网,只为分享网络知识教程,供大家学习参考!

「自学哈网」三分钟学会WordPress接入熊掌号及页面改造

作者 : 自学哈 本文共2150个字,预计阅读时间需要6分钟 2022-11-26 共123人阅读
也想出现在这里? 联系我们

最近想搞个熊掌号来玩玩,于是在前两天申请并且通过.申请通过后需要对网站进行改造,于是翻阅互连网,东拼西凑终于搞定,完成改造,下面分享下页面改造过程.改造之前需注册熊掌号,点此打开,申请次数有限,申请时注意手持身份证照片最好是原图,避免系统识别不到身份证信息.不同WordPress主题改造文件有所不同,这里我当前使用知更鸟Lts版本,需用到的3个文件分别为functions.php、head.php、content.php

首先使用到的是head.php,添加熊掌号ID声明,代码如下:


<script src=“//msite.baidu.com/sdk/c.js?appid=此处填写熊掌号ID”></script>

 

添加JSON_LD数据


<!–Start–>
<!–熊掌号–>
<script src=“//msite.baidu.com/sdk/c.js?appid=1595281659287879”></script>
<script type=“application/ld+json”>
        {
            “@context”“https://ziyuan.baidu.com/contexts/cambrian.jsonld”,
            “@id”“<?php the_permalink(); ?>”,
            “appid”“熊掌号ID”,
            “title”“<?php the_title(); ?>”,
            “images”: [“<?php echo fanly_post_imgs();?>”],
            “description”“<?php echo fanly_excerpt()?>”,
            “pubDate”“<?php echo get_the_time(‘Y-m-dTH:i:s’)?>”
        }
</script>
<!–end–>

 

functions.php


//Start
//获取文章/页面摘要
function fanly_excerpt($len=220){
    if ( is_single() || is_page() ){
        global $post;
        if ($post->post_excerpt) {
            $excerpt  = $post->post_excerpt;
        } else {
            if(preg_match(‘/<p>(.*)</p>/iU’,trim(strip_tags($post->post_content,“<p>”)),$result)){
                $post_content = $result[‘1’];
            } else {
                $post_content_r = explode(“n”,trim(strip_tags($post->post_content)));
                $post_content = $post_content_r[‘0’];
            }
            $excerpt = preg_replace(‘#^(?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,0}‘.’((?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,’.$len.’}).*#s’,’$1‘,$post_content);
        }
        return str_replace(array(“rn”“r”“n”), “”$excerpt);
    }
}
//获取缩略图
function fanly_post_imgs(){
    global $post;
    $src = ;
    $content = $post->post_content;
    preg_match_all(‘/<img .*?src=[“|’](.+?)[”|’].*?>/’, $content, $strResult, PREG_PATTERN_ORDER);  
    $n = count($strResult[1]);  
    if($n >= 3){
        $src = $strResult[1][0].’”,“‘.$strResult[1][1].’”,”‘.$strResult[1][2];
    }elseif($n >= 1){
        $src = $strResult[1][0];
    }
    return $src;
}
//END

 

以下代码不同主题可能有所不同,根据实际情况添加适当位置,本站当前在105行处添加,文件目录为主题目录/template/content.php


<script>cambrian.render(‘tail’)</script>

 

这样就大功告成了~~

本站声明:
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!

自学哈网 » 「自学哈网」三分钟学会WordPress接入熊掌号及页面改造
也想出现在这里? 联系我们
© 2022 Theme by - 自学哈网 & WordPress Theme. All rights reserved 浙ICP备2022016594号