首页 > SEO优化 > wordpress相关文章的优化机智和自定义设置

wordpress相关文章的优化机智和自定义设置

时间:2023年10月18日 分类:SEO优化 浏览量:75

之前大叔介绍过 wordpress相关文章实现的方法,例:《代码实现WordPress相关文章》,那么今天说的这个教程,是从优化角度来更合理的实现wordpress相关文章的,至于客观喜欢哪个,自己决定吧!

策略:文章内容相关程度: 手动指定 > 标签 >分类 > 随机

实现方式:下面代码直接加到functions.php中即可

  1. function add_related_posts($content){

  2.     return $content . wp_related_posts();

  3. }

  4. add_filter (‘the_content’, ‘add_related_posts’); //hook

  5. function wp_related_posts(){

  6.     global $post;

  7.     $num = 5;//文章数量

  8.     $counter = 1;

  9.     $exclude_id = get_post_meta($post->ID,’related’,true);//获取手动置顶的相关文章

  10.     if ($exclude_id){

  11.         $args = array(

  12.             ‘post_status’ => ‘publish’,

  13.             ‘post_type’ => array(‘post’),

  14.             ‘post__in’ => explode(‘,’, $exclude_id),

  15.             ‘posts_per_page’ => $num

  16.         );

  17.         $posts = get_posts($args);

  18.         foreach($posts as $sb){

  19.             $output .= ‘<li><a href=”‘ . get_permalink($sb->ID) . ‘”>’ . $sb->post_title . ‘</a></li>’;//可自定义样式

  20.             $i++;

  21.         }

  22.     }

  23.     if( $i < $num){//自定义文章数不足后通过分类和标签处理

  24.         $tagsid = array();

  25.         $catid = array();

  26.         $thisid[] = $post->ID;

  27.         $posttags = get_the_tags();

  28.         $catids = get_the_category();

  29.         if(!emptyempty($posttags)) {

  30.             foreach($posttags as $tag) {

  31.                 $tagsid[] = $tag->term_id;

  32.             }

  33.         }

  34.         if(!emptyempty($catids)) {

  35.             foreach($catids as $cat) {

  36.                 $catid[] = $cat->term_id;

  37.             }

  38.         }

  39.         $args = array(

  40.             ‘post_type’ => ‘post’,

  41.             ‘post__not_in’ => $thisid,

  42.             ‘ignore_sticky_posts’ => 1,

  43.             ‘posts_per_page’ => ($num – $i),

  44.             ‘tax_query’ => array(

  45.                 ‘relation’ => ‘OR’,//改成AND则必须是同标签同分类下

  46.                 array(

  47.                     ‘taxonomy’ => ‘post_tag’,

  48.                     ‘field’    => ‘term_id’,

  49.                     ‘terms’    => $tagsid,

  50.                 ),

  51.                 array(

  52.                     ‘taxonomy’ => ‘category’,

  53.                     ‘field’    => ‘term_id’,

  54.                     ‘terms’    => $catid,

  55.                 ),

  56.             ),

  57.         );

  58.         $rsp = get_posts($args );

  59.         foreach($rsp as $sb){

  60.             $output .= ‘<li><a href=”‘ . get_permalink($sb->ID) . ‘”>’ . $sb->post_title . ‘</a></li>’;//可自定义样式

  61.             $i++;

  62.         }

  63.     }

  64.     $final = ‘<h3>相关文章</h3><ul>’ . $output . ‘</ul>’;

  65.     return $final;

  66. }

调用方法
如需加入自定义相关文章,只需新建自定义栏目,加入文章id即可,多篇文章用,隔开

如想自定位置,并调整样式,则去掉the_content的钩子,然后手动调用wp_related_posts函数

骚年,创作吧。。。。

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

标签: