手把手教你百度熊掌号接入教程二
浏览次数:1091 分类:SEO优化
上周讲到如何操作接入熊掌号的初级教程,今天我们来具体分享下修改相关代码:
一、找到content.php文件,一般在文章熊掌好接入在文章末尾处,先看前端效果:
对应后端代码:(注意是主题模板里的content.php文件),大概是加到43行
<?php else : ?> <div class="single-content"> <?php if ( has_excerpt() ) { ?><span class="abstract"><span>摘要</span><?php the_excerpt() ?><div class="clear"></div></span><?php }?> <?php get_template_part('ad/ads', 'single'); ?> <?php the_content(); ?> <script>cambrian.render('tail')</script> </div>
然后再找到header.php文件,加上你自己改动的自变量,如下:
具体代码:
<!--百度熊掌号添加canonical标签--> <!--百度熊掌号添加JSON_LD数据--> <?php global $wp; $current_url = home_url(add_query_arg(array(),$wp->request)); if($current_url)echo '<link rel="canonical" href="'.$current_url.'" />'."\n"; ?> <?php if( is_single() || is_page() ): ?> <script type="application/ld+json"> { "@context": "https://ziyuan.baidu.com/contexts/cambrian.jsonld", "@id": "<?php echo curPageURL();?>", "appid": "你的熊掌号ID", "title": "<?php echo wp_title('', false); ?>", "images": [ "http://你的域名<?php echo post_thumbnail_src();?>" ], "description": "<?php if ($post->post_excerpt) {$printDescription = $post->post_excerpt;} else{ $printDescription = preg_replace('/\s+/','',mb_strimwidth(strip_tags($post->post_content),0,145,'')); } echo $printDescription;?>", "pubDate": "<?php echo get_the_time('Y-m-d\TG:i:s'); ?>" } </script> <?php endif; ?> <script src="//msite.baidu.com/sdk/c.js?appid=你的熊掌号ID"></script>
最后找到你的function文件,添加这一行代码:
//输出缩略图地址 function post_thumbnail_src(){ global $post; if( $values = get_post_custom_values("thumb") ) { //输出自定义域图片地址 $values = get_post_custom_values("thumb"); $post_thumbnail_src = $values [0]; } elseif( has_post_thumbnail() ){ //如果有特色缩略图,则输出缩略图地址 $thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full'); $post_thumbnail_src = $thumbnail_src [0]; } else { $post_thumbnail_src = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); if(!empty($matches[1][0])){ $post_thumbnail_src = $matches[1][0]; //获取该图片 src }elseif( suxingme('suxingme_post_thumbnail') ){ $post_thumbnail_src = suxingme('suxingme_post_thumbnail'); }else{ //如果日志中没有图片,则显示随机图片 //$random = mt_rand(1, 5); //$post_thumbnail_src = get_template_directory_uri().'/img/random/'.$random.'.jpg'; //如果日志中没有图片,则显示默认图片 $post_thumbnail_src = get_template_directory_uri().'/img/default_thumb.png'; } } return $post_thumbnail_src; }
接下来大功告成
下一篇: 飞鸟MIP页面引入改造及操作教程
您好!请登录