Beep
Beep

Reputation: 2823

Change WordPress loop to show category

I have a simple WordPress loop to get 100 latest posts. How can I change this to show posts dynamically depending on the category I'm on?

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

Upvotes: 0

Views: 221

Answers (1)

Katie
Katie

Reputation: 2683

If you mean that you are on a category archive page, then this code will work:

// 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));

Upvotes: 1

Related Questions