导语:wordpress里增加相关文章的方法有很多,有的是按照标签来实现,有的是按照分类来实现。我分享的这段代码是把标签和分类的方法结合起来了,先查找标签下的文章是否满足数量,如果不满足就找分类里面的文章。
其实这样做也谈不上有多大的好处,唯一的好处就是在建站初期文章比较少的时候,版面内容看上去多。到了后期文章多的时候,分类的文章也就基本不会输出了。
在网站的后期,对于性能的影响应该是比较小的,因为它就做了一次数量判断,发现数量判断为真,就不会执行查找分类文章的代码了。
我们来看看代码:
<div class="entry-related-articles"> <h3>或许您还喜欢这些文章</h3> <div class="related-articles row clearfix"> <?php global $post; $num=6;//输出6篇文章 //$post_tags = wp_get_post_tags($post->ID); $post_tags = wp_get_post_tags($post->ID); if ($post_tags) { foreach ($post_tags as $tag) { // 获取标签列表 $tag_list[] .= $tag->term_id; } // 随机获取标签列表中的一个标签 $post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ]; $custom_query_args = array( 'tag__in' => array($post_tag), 'category__not_in' => array(NULL), // 不包括的分类ID 'post__not_in' => array($post->ID), 'showposts' => $num, // 显示相关文章数量 'ignore_sticky_posts' => 1 , 'orderby' => 'rand' ); $custom_query = new WP_Query( $custom_query_args ); $pnum = 0; if ($custom_query->have_posts()) { while ($custom_query->have_posts()) { $custom_query->the_post(); //update_post_caches($posts); ?> <article <?php post_class( 'col-md-4 col-sm-12' ); ?>> <div class="related-thumb"> <a href="<?php echo esc_url( get_permalink() ); ?>" title="<?php the_title(); ?>"> <?php if ( has_post_thumbnail( ) ) { the_post_thumbnail( 'codilight_lite_block_2_medium' ); } else { echo '<img alt="'. esc_html( get_the_title() ) .'" src="'. esc_url( get_template_directory_uri() . '/assets/images/blank250_170.png' ) .'">'; } ?> </a> <?php $category = get_the_category(); if ( $category[0] ) { echo '<a class="entry-category" href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>'; } ?> </div> <div class="entry-detail"> <header class="entry-header"> <?php the_title( sprintf( '<h2 class="entry-title h5"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?> </header> </div> </article> <?php $pnum =$pnum+1;//每输出一篇加一 }//循环结束 }//如果标签内没有文章 wp_reset_postdata(); // reset the query }//如果没有标签 $i=$num-$pnum; //计算还需要输出了多少篇文章 if ($i!=0){ global $post; $cats = wp_get_post_categories($post->ID); if ($cats) { $custom_query_args = array( 'category__in' => array( $cats[0] ), 'post__not_in' => array( $post->ID ), 'showposts' => $i, 'ignore_sticky_posts' => 1, 'orderby' => 'rand' ); $custom_query = new WP_Query( $custom_query_args ); if ($custom_query->have_posts()) { while ($custom_query->have_posts()) { $custom_query->the_post(); //update_post_caches($posts); ?> <article <?php post_class( 'col-md-4 col-sm-12' ); ?>> <div class="related-thumb"> <a href="<?php echo esc_url( get_permalink() ); ?>" title="<?php the_title(); ?>"> <?php if ( has_post_thumbnail( ) ) { the_post_thumbnail( 'codilight_lite_block_2_medium' ); } else { echo '<img alt="'. esc_html( get_the_title() ) .'" src="'. esc_url( get_template_directory_uri() . '/assets/images/blank250_170.png' ) .'">'; } ?> </a> <?php $category = get_the_category(); if ( $category[0] ) { echo '<a class="entry-category" href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>'; } ?> </div> <div class="entry-detail"> <header class="entry-header"> <?php the_title( sprintf( '<h2 class="entry-title h5"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?> </header> </div> </article> <?php } }//如果分类里没有其他的问题 wp_reset_postdata(); // reset the query }//如果没有分类 }//如果标签没有输出6篇文章,开始输出分类文章 ?> </div> </div>
注意的是,这里我用了WP_Query()来获取文章,有些代码里是用query_posts()来获取文章。query_posts()在其他地方使用,有可能会影响主循环,用WP_Query()来获取比较好。
我这里设置了输出6篇文章,先输出相关标签里的文章,如果还没有达到6篇,那么再输出分类里的文章。
注意,有可能会输出相同的文章,如果一篇文章存在于同一标签内,也在同一分类内,就有可能输出两次。当然,这种现象只会出现在文章少需要分类来凑数的时候,后期文章多了,就不会出现了。
这个文章的特色图片我用了文章和姚笛的剧照,我找相关文章的图片的时候找到了文章。大家一笑。