随机文章(无插件)

2009年4月30日 | 分类: 電腦樂園 | 标签: , | 浏览: 949 次阅读

教你如何在侧栏里加入随机文章,当然我说的是不用插件的情况下。

首先,在sidebar.php里找以下代码:

<!– recent posts –>
<li id=”pages” class=”widget widget_pages”>
<h3>Recent Posts</h3>
<ul>
<?php $posts = get_posts(‘numberposts=10&orderby=post_date’); foreach($posts as $post) : setup_postdata($post); ?>
<li>
<span class=”sidedate”><?php the_time(‘y/m/d’) ?></span>
<a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a>
</li>
<?php endforeach; $post = $posts[0]; ?>
</ul>
</li>

把其中的 orderby=post_date改成orderby=rand,这样就成随机文章了。numberposts=10其中10代表要显示多少条随机文章。以下是我的随机文章代码:

<!– random posts –>
<li id=”pages” class=”widget widget_pages”>
<h3>隨機文章</h3>
<ul>
<?php $posts = get_posts(‘numberposts=5&orderby=rand’); foreach($posts as $post) :   setup_postdata($post); ?>
<li>
<span class=”sidedate”><?php the_time(‘y/m/d’) ?></span>
<a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a>
</li>
<?php endforeach; $post = $posts[0]; ?>
</ul>
</li>

注意:改好的随机文章代码不能放在代码 <?php if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar() ) : ?><?php endif; ?>之间,不然会无效也就是说显示的还是最近文章。


声明: 原创文章,转载请注明转自 8suo -> 随机文章(无插件)
  1. 2009年4月30日21:52

    这个写的不错,我那主题默认是有这功能,但是橱窗添加了内容以后就消失了,后来就找来了PAGEVIEW,变成了热点信息常年不变,哈哈。

  2. 2009年4月30日22:10

    你的博客热点好过随机,软件越热越好,越新越好。

  3. 2009年5月5日13:59

    感觉随机好一点,否则老博文就不见天日了

  4. 2009年5月5日14:06

    但对于软件站,软件越新越好,从而博文越新越好。

注意: 评论者允许使用'@user空格'的方式将自己的评论通知另外评论者。