【问题标题】:Wordpress WP_query returns only the current page regardless of argumentsWordpress WP_query 只返回当前页面而不考虑参数
【发布时间】:2018-03-09 21:16:58
【问题描述】:

在我的 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 wordpress


    【解决方案1】:

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

    <?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;?>
    

    【讨论】:

    • Ahhh omfg... 是的,我注意到了不同之处,并且我知道这是我所缺少的一些可笑的基本内容。感谢您快速回复您和所有其他答案!
    【解决方案2】:

    试试这个:

    <?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;?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-15
      • 1970-01-01
      • 1970-01-01
      • 2015-03-07
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多