Home > Uncategorized > WordPress 随机日志调用代码

WordPress 随机日志调用代码

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>

Categories: Uncategorized Tags: ,
  1. No comments yet.
  1. No trackbacks yet.
请注意:如果您回复的跟博文无关,一律当成垃圾评论,不要心存侥幸,以免浪费大家的时间!谢谢