这个还蛮不错的,相关文章我觉得是提升博客导航的一个很好的方式,以下是本站使用的代码,大家可以放在主题正文模板底部的位置。
<!-- 相关文章开始 -->
<ul class="spostinfo"> <h3>80%的人都看过的文章</h3> <ul class="related_posts"> <?php $post_num = 8;
$exclude_id = array($post->ID);
$posttags = get_the_tags();
$cats = get_the_category();
$args = array( 'post_status' => 'publish', 'post__not_in' => $exclude_id, 'orderby' => 'comment_date', 'posts_per_page' => $post_num, );
if ($posttags) {
$tag_ids = array();
foreach ($posttags as $tag) {
$tag_ids[] = $tag->term_id;
}
$args['tag__in'] = $tag_ids;
} elseif ($cats) {
$cat_ids = array();
foreach ($cats as $cat) {
$cat_ids[] = $cat->term_id;
}
$args['category__in'] = $cat_ids;
}
$related_posts = new WP_Query($args);
if ($related_posts->have_posts()): while ($related_posts->have_posts()) : $related_posts->the_post();
?> <li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title();
?></a></li> <?php endwhile;
wp_reset_postdata(); else: echo '<li>没有相关文章!</li>';
endif;
?> <br/> </ul>
<!-- 相关文章结束 -->
80%的人都看过的文章
本文来自凡蜕博客(https://blog.ysboke.cn), 转载请带上地址.。