wordpress调取随机文章主要有以下两种循环方式:
方法一:foreach循环
- <?php
- $rand_posts = get_posts('numberposts=10&orderby=rand');
- foreach( $rand_posts as $post ) :
- ?>
- <!–下面是你想自定义的Loop–>
- <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
- <?php endforeach; ?>
方法二:while循环
- <?php
- $post_num = ''.sc_tsxcc('ts_syshulaing').''; // 设置调用条数
- $args = array(
- 'caller_get_posts' => 1, // 排除置顶文章.
- 'orderby' => rand, // 依评论数排序.
- 'posts_per_page' => $post_num
- );
- query_posts($args); ?>
- <?php while (have_posts()) : the_post(); ?>
- <li>
- <a target="_blank" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="title"><?php the_title(); ?></a>
- </li>
- <?php endwhile; ?>
tsxc主题使用的第二种方法,后台设置调取随机文章的数量。
这2种方法都能实随机文章的调用。