A-A+
WordPress随机文章调用
上一篇了解了 wordpress 最新文章的调用,这一节来了解下 wordpress 随机文章的调用,最新文章与随机文章都是 wordpress 不可缺少的功能,几乎所有的主题都自带了 wordpress 随机文章。
foreach 遍历数组实现随机文章
<?php $rand_posts = get_posts("numberposts=10&orderb y=rand");foreach( $rand_posts as $post ): ?> <!--下面是你想自定义的Loop--> <li> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a> </li> <?php endforeach; ?>
CSS可以自定义,将上面这段代码放入到 wordpress 主题中就可以了。有些情况下,get_posts会失效,比如直接包含本文件,循环显示的不是随机文章,而是全部相同的文章。只要全局声明下即可,代码如下:
<ul> <?php global $post;$rand_posts = get_posts("numberposts=5&orderby=rand");fo reach( $rand_posts as $post ) :?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?></ul>