【问题标题】:Wordpress: Get posts from category from custom queryWordpress:从自定义查询中获取类别的帖子
【发布时间】:2018-06-29 09:13:11
【问题描述】:

您好,我有以下自定义 wordpress 查询。这可以很好地显示所有博客文章。

<?php $mymain_query = new WP_Query( array( 'post_type' => 'post','posts_per_page' => '10' ) ); while($mymain_query->have_posts()) : $mymain_query->the_post(); ?>

    //shortened code below
    <div class="blog-post">
        <h5><?php the_title(); ?></h5>
        <p><?php the_content(): ?></p>
    </div>

<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>

但是当我将其插入archive.php 时,它仍然调用所有博客文章,而不是该类别中的文章。

谁能建议如何编辑我的代码以仅显示该特定类别的博客文章?

谢谢!

【问题讨论】:

    标签: php wordpress blogs posts


    【解决方案1】:

    我想通了。这是我的解决方案

    <?php 
        $categories = get_the_category();
        $category_id = $categories[0]->cat_ID;
        $mymain_query = new WP_Query( array( 'cat' => $category_id,'posts_per_page' => '10' ) ); while($mymain_query->have_posts()) : $mymain_query->the_post(); ?>
    
        //shortened code below
        <div class="blog-post">
            <h5><?php the_title(); ?></h5>
            <p><?php the_content(): ?></p>
        </div>
    
    <?php endwhile; ?>
    <?php wp_reset_postdata(); // reset the query ?>
    

    【讨论】:

      猜你喜欢
      • 2013-09-15
      • 2021-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多