Home » WP使用, 进阶使用 » 主题制作小技巧之四:非插件方式实现相关日志调用

主题制作小技巧之四:非插件方式实现相关日志调用

如果想在日志页实现相关日志调用,我们以前一般都是使用WordPress Related Posts这个插件来实现,如果你不想使用这个插件,那今天就说另一种不用插件实现相关日志的调用:
打开你的single.php或page.php页面,在任何你想实现相关日志调用的地方加上以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
//显示
$tags = wp_get_post_tags($post->ID);
if ($tags) {
  echo 'Related Posts';
  $first_tag = $tags[0]->term_id;
  $args=array(
    'tag__in' => array($first_tag),
    '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(); ?>
      <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
      <?php
    endwhile;
  }
}
?>

其中“’showposts’=>5”中的5是你想调用的日志数,可以修改!

Leave a comment