WP; get_sidebar not playing nice with get_content when inside loop querying pages

So I hope that title isn’t too confusing, let me try to break it down.

I have a content-page.php file which has both the get_content and get_sidebar functions in it. If I navigate to that page on the front end, I see both my content and sidebar.

Read More

On the home.php page, I’m loading a series of pages by using $the_query = new WP_Query and then inside that query using the loop and inside that loop, calling the content-page.php.

The problem is that if the page that loads on the home.php page has a sidebar, for some reason, nothing loads after the sidebar ie. get_content() returns nothing, comments_template() returns nothing, etc.

Here’s a (very) simplified version of the markup, Homepage:

$the_query = new WP_Query( array(
    'post_type' => 'page'));
    $x = 0;
    while ( $the_query->have_posts() ) :
        $the_query->the_post();

        get_template_part( 'content', 'page' );

    $x++;
    endwhile;
wp_reset_query();

content-page.php:

<article <?php post_class(); ?>>  
    <header>
      <h1><?php the_title(); ?></h1>
    </header>

    <div class="entry-content">
        <?php include( TEMPLATEPATH . '/sidebar.php'); ?>
        <?php the_content(); ?>
    </div><!-- .entry-content -->
</article><!-- #post-<?php the_ID(); ?> -->

sidebar.php:

<div id="secondary" class="widget-area" role="complementary">
    <?php do_action( 'before_sidebar' );
    dynamic_sidebar( 'sidebar-1' ); ?>
</div><!-- #secondary .widget-area -->

Is there something obvious that I’m missing here? Been working on this one for a while. As always, any help is greatly appreciated.

Related posts

4 comments

  1. There are two types of general Loop queries in WordPress:

    1. the main query, which is set based on the HTTP request, and
    2. ‘sub loops’ which can change the main query.

    Since the main query controls many things like “is there a sidebar?” or “what page template should be used?” you can only submit one of these types. You can’t call a separate sidebar (or call a separate page template for that matter) from within a sub loop. To do this, you would have to re-do the basic WordPress sidebar system.

    I think the best bet for what you are trying to do, would be to just serve up some kind of dynamic content and call it a sidebar. In other words, WordPress has already decided what’s happening with the sidebar the first time it was called. The system doesn’t have a default way to handle calling the sidebar over and over. Using a shortcode or a filter after each iteration of the sub loop would be a better approach.

  2. Close “)” open brackets.

    $the_query = new WP_Query( array( 'post_type' => 'page') );
    
  3. Paste this code in content-page.php

    <?php get_template_part( 'sidebar' ); ?>
            <?php the_content(); ?>
    

    Try removing do_action( 'before_sidebar' );

  4. You could try:

    include( locate_template( 'content-page.php', false, false ) );

    instead of:

    get_template_part('content', 'page');

Comments are closed.