wordpress获取随机文章的方法有很多,但我认为最好,最符合wordpress开发模式的是以下的代码:
<ul> <?php $args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish' ); $rand_posts = get_posts( $args ); foreach( $rand_posts as $post ) : ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul>
核心函数是$args = array( ‘numberposts’ => 5, ‘orderby’ => ‘rand’, ‘post_status’ => ‘publish’ );