如何在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)  代表随机显示的文章中会排除当前文章。

1 Comment

  1. AnQ says:

    不错,很实用的方法

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

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

无觅相关文章插件,快速提升流量

任鸟飞网页设计博客 谜题推理 No.1 Web Design Gallery IndustryIdea LookForDesign AWebGallery Mobile Web Template iDesign Wallpaper Big Funny Picture iPhone Faves iPhone Mobi iPad Faves Android Faves BlackBerry Faves Best Design Magazine All Banner Templates Blog Earn Tips 是谁啊 只爱美人 家乡美 唯美爱 美人衣妆 AutoCAD 2D | Pro/E 3D江苏名企The HTML5 Templates