<?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 篇文章,列表样式可以根据需要自定义。
比如加上表头:
<h3>随机日志</h3>
<ul>
......(上面的随机代码)
</ul>
没有评论:
发表评论