3

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?

4
  • are you aiming to use static page per category with a custom page template and custom loop for this?
    – Michael
    Apr 19 '17 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. Apr 19 '17 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? Apr 19 '17 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. Apr 19 '17 at 20:12
1

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>';
            }
        }
    }

?>
2
  • Holy cow dude, this worked! I had to modify it for what I needed it for, obviously, but this helped me understand the logic behind the query. Thank you! Apr 19 '17 at 23:16
  • Happy to help! Please click the check mark next to this answer to close the question. Thank you! Apr 19 '17 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.