有两种方法(两种实现函数),不过原理都差不多,获取优先级为:标签--分类--随机
方法一
自定义函数 inlo_related_acticles
然后在指定位置 echo
输出调用
//相关文章 function inlo_related_acticles($post_id = 0) { $posts = $terms = array (); $args = array ( 'post_status' => 'publish', 'post__not_in' => array ( $post_id ), 'caller_get_posts' => 1, 'orderby' => 'rand', 'posts_per_page' => 6 ); // 与标签相关的文章 $post_terms = wp_get_post_terms ( $post_id, 'post_tag' ); if (! empty ( $post_terms )) { foreach ( $post_terms as $term ) { $terms [] = $term->term_id; } $args ['tag__in'] = $terms; $posts = query_posts ( $args ); wp_reset_query (); } // 如果没有与标签相关的文章,获取与分类相关的文章 if ( empty($posts) ) { unset ( $args ['tag__in'] ); $post_terms = wp_get_post_terms ( $post_id, 'category' ); if (! empty ( $post_terms )) { foreach ( $post_terms as $term ) { $terms [] = $term->term_id; } $args ['category__in'] = $terms; $posts = query_posts ( $args ); wp_reset_query (); } } // 如果相关文章不够6篇,获取随机文章 $count = count ( $posts ); if ( $count < 6 ) { $post_not_in = array(); if( !empty($posts) ) { // 若存在文章 foreach ($posts as $post) { // 遍历传值给post_not_in数组 $post_not_in[] = $post->ID; } } $args = array ( // 再定义一遍数组,如果不定义就无法获取随机文章了 'post__not_in' => array ( $post_id ), //排除的文章,采用id形式 'orderby' => 'rand', // 随机文章 ); unset ( $args ['category__in'] ); $args ['posts_per_page'] = 6 -$count; // 缺少的篇数(用随机文章补上) $args ['post__not_in'] = $post_not_in; // 不包含已经存在的文章 $rand_posts = query_posts( $args ); // 按上面两个条件 查询$args数组输出文章 $posts = array_merge($posts, $rand_posts); // 合并数组 wp_reset_query (); // 结束查询 } $count = count ( $posts ); $str = ''; foreach ( $posts as $k => $p ) { $max_width = ($count == 1 || $count == 5) && $k == $count - 1 ? ' max' : null; $str .= ' <div class="r-post ' . $max_width . '"> <a title="' . $p->post_title . '" href="' . get_permalink ( $p->ID ) . '" rel="bookmark"> <p> <span><svg class="icon" style="width:20px;height:20px;fill:#aaa;position:relative;top:6px;"><use xlink:href="#icon-rpost"></use> </svg></span> ' . inlo_substr ( $p->post_title, 600 ) . '</p> <div class="clear"></div> </a> </div>'; } return $str; }
方法二
来自大发,可以自定义栏目调用指定文章id为最高级--标签--分类--随机
function add_related_posts($content){ return $content . wp_related_posts(); } add_filter ('the_content', 'add_related_posts'); //hook function wp_related_posts(){ global $post; $num = 5;//文章数量 $counter = 1; $exclude_id = get_post_meta($post->ID,'related',true);//获取手动置顶的相关文章 if ($exclude_id){ $args = array( 'post_status' => 'publish', 'post_type' => array('post'), 'post__in' => explode(',', $exclude_id), 'posts_per_page' => $num ); $posts = get_posts($args); foreach($posts as $sb){ $output .= '<li><a href="' . get_permalink($sb->ID) . '">' . $sb->post_title . '</a></li>';//可自定义样式 $i++; } } if( $i < $num){//自定义文章数不足后通过分类和标签处理 $tagsid = array(); $catid = array(); $thisid[] = $post->ID; $posttags = get_the_tags(); $catids = get_the_category(); if(!empty($posttags)) { foreach($posttags as $tag) { $tagsid[] = $tag->term_id; } } if(!empty($catids)) { foreach($catids as $cat) { $catid[] = $cat->term_id; } } $args = array( 'post_type' => 'post', 'post__not_in' => $thisid, 'ignore_sticky_posts' => 1, 'posts_per_page' => ($num - $i), 'tax_query' => array( 'relation' => 'OR',//改成AND则必须是同标签同分类下 array( 'taxonomy' => 'post_tag', 'field' => 'term_id', 'terms' => $tagsid, ), array( 'taxonomy' => 'category', 'field' => 'term_id', 'terms' => $catid, ), ), ); $rsp = get_posts($args ); foreach($rsp as $sb){ $output .= '<li><a href="' . get_permalink($sb->ID) . '">' . $sb->post_title . '</a></li>';//可自定义样式 $i++; } } $final = '<h3>相关文章</h3><ul>' . $output . '</ul>'; return $final; }
如需加入自定义相关文章,只需新建自定义栏目,加入文章id即可,多篇文章用,隔开。
如想自定位置,并调整样式,则去掉the_content
的钩子,然后手动调用wp_related_posts
函数。
本站文章除注明转载/出处外,均为本站原创或翻译。若要转载但请务必注明出处,尊重他人劳动成果。
转载请注明出处链接 : https://www.inlojv.com/4778.html