用代码为wordpress添加相关文章

根据标签来显示相关文章。

将以下代码加入single.php中适当位置。根据主题不同需要做适当调整。

并且这种方法可以根据多个标签来显示相关,不再是根据第一个标签来显示了。

修改第12行’showposts’=>5中5的数值,可以设定显示数量的上限。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
 $tags = wp_get_post_tags($post->ID);
 $tagIDs = array();
if ($tags) {
 $tagcount = count($tags);
   for ($i = 0; $i < $tagcount; $i++) {
      $tagIDs[$i] = $tags[$i]->term_id;
    }
   $args=array(
      'tag__in' => $tagIDs,
     'post__not_in' => array($post->ID),
      'showposts'=>5,
      'caller_get_posts'=>1
    );
   $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
      <?php endwhile;
    }
  }
?>

以上参考:http://fishyoyo.com/5-wordpress-related-posts-plugins.html





本文来源:随便翻翻
原文地址:http://anyff.com/2009/08/%e6%b7%bb%e5%8a%a0%e7%9b%b8%e5%85%b3%e6%96%87%e7%ab%a0/

你可以留言,或者trackback 从你的网站

没有评论

还没有评论呢。

Leave a Reply