WordPress添加相关文章列表

  • A+
所属分类:WordPress

这是WordPress大学版本,测试通过。

在single文件适当位置加入以下代码6-46行。$post_num = 8;为调用相关文章数量,可修改。

<h3>相关文章</h3>
<ul class="related_posts">
<?php
$post_num = 8;
$exclude_id = $post->ID;
$posttags = get_the_tags(); $i = 0;
if ( $posttags ) {
 $tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ',';
 $args = array(
  'post_status' => 'publish',
  'tag__in' => explode(',', $tags),
  'post__not_in' => explode(',', $exclude_id),
  'caller_get_posts' => 1,
  'orderby' => 'comment_date',
  'posts_per_page' => $post_num,
 );
 query_posts($args);
 while( have_posts() ) { the_post(); ?>
  <li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
 <?php
  $exclude_id .= ',' . $post->ID; $i ++;
 } wp_reset_query();
}
if ( $i < $post_num ) {
 $cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
 $args = array(
  'category__in' => explode(',', $cats),
  'post__not_in' => explode(',', $exclude_id),
  'caller_get_posts' => 1,
  'orderby' => 'comment_date',
  'posts_per_page' => $post_num - $i
 );
 query_posts($args);
 while( have_posts() ) { the_post(); ?>
  <li><a rel="bookmark" href="<?php the_permalink(); ?>"  title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
 <?php $i++;
 } wp_reset_query();
}
if ( $i  == 0 )  echo '<li>没有相关文章!</li>';
?>
</ul>

样式表

.related_posts{margin-top:5px;}
.related_posts li{margin-left:20px;color:#444;list-style:circle;font-size:14px;line-height:26px;padding:0 0 0 5px}

或者将代码另存为relatepost.php文件,直接在single.php引用。

<h3>相关文章</h3>
<ul class="related_posts">
<?php include('fengerelatepost.php'); ?>
</ul>
weinxin
独角兽驿站
公众号

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: