如何在WordPress中随机显示文章

随机文章通常会显示在每篇文章的结尾或侧边栏,使文章内容更加充实,用户根据兴趣爱好自由选择。

添加随机文章代码如下

<?php   global $post;
 $postid = $post->ID;
 $args = array(
 'orderby' => 'rand',
 'showposts'=>5,
 'post__not_in'=>array($postid)  );
  query_posts($args);
  echo '<ul>';
  while (have_posts()) : the_post();
  echo '<li><a href="'.get_permalink().'" title="'.the_title('','',false).'">'.the_title('','',false).'</a></li>';
  endwhile;
  echo '</ul>';
 ?>

代码说明:

‘post__not_in’=>array($postid)  代表随机显示的文章中会排除当前文章。

除非注明,本博客文章均为原创,转载请以链接形式标明本文地址
原创文章如转载,请注明:转载自 任鸟飞网页设计 [ http://www.renniaofei.com/ ]
本文链接地址: http://www.renniaofei.com/code/display-random-posts-in-wordpress/
引用通告地址 Trackback URL:直接在本文链接地址后添加 trackback/
本文短网址: http://bit.ly/bVKZnQ
任鸟飞

任鸟飞网页设计网创办者 主要关注网页设计,网站建设,图形图像设计。 Twitter @renniaofei QQ:29900684

1 条评论

  1. AnQ 一月 30th, 2010   2:07 上午

    不错,很实用的方法

留下评论

:wink: :-| :-x :twisted: :) 8-O :( :roll: :-P :oops: :-o :mrgreen: :lol: :idea: :-D :evil: :cry: 8) :arrow: :-? :?: :!: