首页 > 新闻资讯 > 如何在WordPress中不借助插件显示“相关日志”

如何在WordPress中不借助插件显示“相关日志”

0条评论
标签:
更多

“相关日志”是增加博客粘性的好方法,它能让用户在你的博客上停留更长时间。有一些插件能实现这个功能,可如果你不想用插件的话,下文将告诉你如何做。

以下代码基于WordPress中的tag系统来判断日志相关性,请把代码粘贴到相应的loop循环中。

<?php
//for use in the loop, list 5 post titles related to first tag on current post
$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;
  }
}
?>

大功告成!

原文:How to: Show related posts without a plugin

* 版权声明:作者WordPress啦! 转载请注明出处。

发表新评论

此内容将保密,不会被其他人看见。
  • 自动将网址与电子邮件地址转变为链接。
  • 允许HTML标签:<a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • 自动断行和分段。

更多关於格式化选项的信息

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.