WordPress自定义循环查询文章代码

wp_query的使用方法,可以参考这篇文章 http://junyiseo.com/wordpress/155.html

 
<?php
$args = array('cat' =>'5' , //这里数组的用法可以看wp-query的详细解析
            'posts_per_page' =>'5' ,
 
    );
 
$the_query = new WP_Query( $args);
 
// 开始循环
if ( $the_query->have_posts() ) {//如果找到了结果,便输出以下内容
 
    while ( $the_query->have_posts() ) {//再次判断是否有结果
        $the_query->the_post();//不用问为什么,每次都要写这个;
 
        ?>
 
        <li><?php the_title();?></li>//这里写上我们需要循环的html代码
 
 
        <?php
    }
 
} else {
    // 如果没有找到任何结果,就输出这个
}
 
wp_reset_postdata();//不用问为什么,每次都记得写就好
?>
Leave a Reply

Your email address will not be published. Required fields are marked *