Loop with Dynamic Categories

admin2025-06-06  0

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?

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?

Share Improve this question edited Apr 19, 2017 at 21:56 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Apr 19, 2017 at 18:51 Nathan AlvarezNathan Alvarez 311 silver badge2 bronze badges 4
  • are you aiming to use static page per category with a custom page template and custom loop for this? – Michael Commented Apr 19, 2017 at 18:52
  • @Michael yes. I have several pages created that have no content, but I need a template that I can assign to each of these pages that will pull associated posts for matching categories for each page. – Nathan Alvarez Commented Apr 19, 2017 at 18:55
  • I've done something similar before where I associated pages with the category taxonomy and created a special page template that would show the page's content as well as the posts associated with the category assigned to the page. The user would need to assign the category and select the appropriate template in the admin area. This allowed us to ensure that the correct category was assigned (if done completely dynamically, based on a page slug and category slug match for example, there is room for error if the slugs don't exactly match). Would this kind of approach be suitable for you? – Dave Romsey Commented Apr 19, 2017 at 19:49
  • @DaveRomsey this was what was suggested to me, but I suppose I'm having trouble figuring out how to associate pages with categories in my template so that, regardless of what page is pulled up, the correct category and associated posts would be displayed. – Nathan Alvarez Commented Apr 19, 2017 at 20:12
Add a comment  | 

1 Answer 1

Reset to default 2

The code below enables categories for pages. An example page template is provided which loops through the categories assigned to the page and displays the posts for each category.

If you want to limit the user to selecting only one category, you can use a solution such as Taxonomy Single Term.

Associate the category taxonomy with the page post type:

function wpse_page_category() {
    register_taxonomy_for_object_type( 'category', 'page' );    
}
add_action( 'init', 'wpse_page_category', 999  );

Example bare bones page template (template-page-categories.php):

<?php
/**
 * Template Name: Page Categories
 *
 */

get_header(); ?>


<?php 
    // Standard loop for page content
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post();
            the_title( '<h1>', '</h1>' );
            the_content();
        }
    }


    // Get the category assigned to this page and list the posts in this category.
    // This code works when multiple categories have been assigned to the page.
    $page_categories = get_the_terms( get_the_ID(), 'category' );
    if ( $page_categories && ! is_wp_error( $page_categories ) ) {

        foreach ( $page_categories as $page_category ) {

            $posts_query = new WP_Query( [
                'post_type' => 'post',
                'cat' => $page_category->term_id,
            ] );


            if ( $posts_query->have_posts() ) {
                echo '<h2> Posts from the <em>' . esc_html( $page_category->name ) . '</em> category:</h2>';
                while ( $posts_query->have_posts() ) {
                    $posts_query->the_post();
                    the_title( '<h3>', '</h3>' );
                    //the_content();
                }
                echo '<hr>';
            }
        }
    }

?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749209885a317281.html

最新回复(0)