繁体   English   中英

WordPress:从自定义查询中获取类别中的帖子

[英]Wordpress: Get posts from category from custom query

您好,我有以下自定义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 
    $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 ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM