12 次查询 耗时 0.054 秒
本文共计1600个字,0条留言,预计阅读时长6分钟
登录/注册
  • 首页
  • >
  • Notes
  • >
  • WordPress优化:熊掌号页面改造
  • WordPress优化:熊掌号页面改造

    作者:Misshylsay

    日期:2020年02月18日

    我们在做WordPress优化的时候,其中有一项是不容忽视的,那就是百度熊掌号,百度熊掌号登录网址:https://xiongzhang.baidu.com

    但是百度熊掌号对页面改造是有要求的,你必须符合他们的要求之后才能通过API提交资源,下面给大家介绍一种关于WordPress熊掌号页面改造的简单方法:

    
    //获取文章/页面摘要
    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("\r\n", "\r", "\n"), "", $excerpt);
    }
    }
     
    //获取文章中的图 last update 2018/01/22
    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;
    }
     
    //熊掌号h5页面改造
    function fanly_h5(){
    if(is_single()){
    echo '<script src="//msite.baidu.com/sdk/c.js?appid=你自己的熊掌号id"></script>';
    echo '<script type="application/ld+json">{
    "@context": "https://ziyuan.baidu.com/contexts/cambrian.jsonld",
    "@id": "'.get_the_permalink().'",
    "appid": "你自己的熊掌号id",
    "title": "'.get_the_title().'",
    "images": ["'.fanly_post_imgs().'"],
    "description": "'.fanly_excerpt().'",
    "pubDate": "'.get_the_time('Y-m-d\TH:i:s').'"
    }</script>
    ';}
    }

    把以上代码放入主题的function.php中,然后在header.php中的</head>标签之前放入如下代码即可。

    <?php fanly_h5();?>

    是不是很简单?快去试一下吧。

    “WordPress优化:熊掌号页面改造”共有0条评论

    发表评论

    B em del U Link Code Quote