首页 > 解决方案 > 更改 WordPress 循环以显示类别

问题描述

我有一个简单的 WordPress 循环来获取 100 个最新帖子。如何更改此设置以根据我所在的类别动态显示帖子?

  <?php
    // the query
    $wpb_all_query = new WP_Query(array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'post-no' => 100));
    ?>
    <?php if ($wpb_all_query->have_posts()) :
        while ($wpb_all_query->have_posts()) : $wpb_all_query->the_post(); ?>
            <img class="card-img-top bottom-line mb-2 lozad "
                             data-src="<?php the_post_thumbnail_url(); ?>"
                             alt="<?php the_title(); ?>"/>
        <?php endwhile; ?>
    <?php else : ?>

标签: phpwordpressloops

解决方案


如果您的意思是您在类别存档页面上,那么此代码将起作用:

// gets the category information from the archive page
$cat = get_the_category();

// the query
$wpb_all_query = new WP_Query(array(
    'post_type' => 'post',
    'post_status' => 'publish',
    // queries for posts that have that category
    'cat' => $cat[0]->cat_ID,
    'post-no' => 100));

推荐阅读