二十六、Wordpress中get_posts()方法的使用,随机调用文章
get_posts() //通过参数获取对应的文章(具体参数百度这个函数)
<div class="sbox"> <h2>随机文章</h2> <? //参数数组 $args = array( 'numberposts' => 3, 'category' => '1', 'orderby' => 'rand' ); $rand_posts = get_posts( $args ); //通过参数获取对应的文章 ?> <ul> <? foreach ( $rand_posts as $post ) { //循环调用出的文章 setup_postdata( $post ); ?> //将自定义查询的文章对象声明到全局$post变量中,以便使用模板标签 //显示当前被处理文章的链接 //显示当前文章的标题 <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <? } wp_reset_postdata(); ?> //还原$post全局变量 </ul> </div>