Wordpress WP_query只返回当前页面,而不考虑参数



在我的wordpress首页,我正在使用query_posts来显示博客中的帖子。

但是,我认为使用 query_posts 是一种不好的做法,所以我使用 WP_query 重写代码。问题是WP_query只在我这样做时返回当前页面,不管我明确告诉wordpress查找帖子而不是页面的事实:

<?php $que = new WP_query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>2));
    if(have_posts()): while(have_posts()): the_post(); ?>
        <a href="<?php the_permalink(); ?>">
            <p class="date">
                <?php the_date();?>
            </p>
            <h4>
                <?php the_title();?>
            </h4>
        </a>
<?php endwhile; endif;?>

你的代码不正确,试试这个(你注意到区别了吗?

<?php 
$the_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>2));
    if($the_query->have_posts()): while($the_query->have_posts()): $the_query->the_post(); ?>
        <a href="<?php the_permalink(); ?>">
            <p class="date">
                <?php the_date();?>
            </p>
            <h4>
                <?php the_title();?>
            </h4>
        </a>
<?php endwhile; endif;?>

试试这个:

<?php $que = new WP_query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>2));
    if($que->have_posts()): while($que->have_posts()): $que->the_post(); ?>
        <a href="<?php the_permalink(); ?>">
            <p class="date">
                <?php the_date();?>
            </p>
            <h4>
                <?php the_title();?>
            </h4>
        </a>
<?php endwhile; endif;?>

最新更新