WordPress Loop with Dynamic Categories

I have a need to create a WordPress loop that displays all of the posts associated with a category, but I need that category to match whichever page I’m viewing.

For example: Let’s say I have Category 1 with all of the posts I want displayed on Page 1. When I go to Page 2, I want that category to change to Category 2 so all of those associated posts display on Page 2.

Current, my loop looks like this:

<?php query_posts('$cat_ID'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <?php the_content(); ?>

    <?php endwhile; else: ?>

        <p>Sorry, this page does not exist</p>

    <?php endif; ?>
</div>

Obviously this is going to display all posts regardless of Category. I need to make sure I have the category change depending on the page.

Any suggestions?

Read more here: WordPress Loop with Dynamic Categories

Leave a Reply

Your email address will not be published. Required fields are marked *