WordPress 随机日志调用代码

This entry was posted by 老猫 Thursday, 20 August, 2009

Google 首页有“手气不错”按钮,如果需要为 WordPress 添加随机文章链接,简单的代码就能实现,并不需要插件支持。每次刷新页面,链接会自动更新,演示效果请点击芒果首页的蓝色按钮。

随机文章代码如下:

<?php $rand_post=get_posts('numberposts=1&orderby=rand'); foreach($rand_post as $post) : ?><a href="<?php the_permalink(); ?>">随机</a><?php endforeach; ?>

以上代码使用 WordPress 默认函数 get_posts 中的 orderby=rand 属性来随机选取文章链接。但如果需要调用多篇文章并以列表形式展示,则代码如下:

<?php $rand_post = get_posts('numberposts=10&orderby=rand');
foreach( $rand_post as $post ) : ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>

以上代码随机选择 10 篇文章,列表样式可以根据需要自定义。

转载自 <a href=”http://www.mangguo.org/wordpress-random-post-call-code/” title=”WordPress 随机日志调用代码” rel=”bookmark”>WordPress 随机日志调用代码 | 芒果</a>

One Response to “WordPress 随机日志调用代码”

  1. 正在寻找这样的代码,不喜欢用插件


Leave a Reply