WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Multiple Loops not showing the_content in 2nd loop (2 posts)

  1. erikas_boy
    Member
    Posted 1 year ago #

    Okay, I'm working on a site built using a custom child-theme of thematic. On a specific page, I'm hooking a loop above the content, and a second one below the page content. The first loop shows the most recent post in the taxonomy (perfect-pint) and the second one shows the next 4 most recent. When I did it just displaying the titles (get_the_title) and building a link around it with get_permalink, everything worked perfectly. All five titles showed where they should. When I also added the_content in the first loop, it went fine. However, when I added the same code to the second loop, each title displayed, but the content it displayed was the content from the page it was on, not the content from each post. Just the same main content looped four times under four different titles.

    I'm working on my local server, but could upload it to a testing site if that would help. Here is the code from my functions.php as it stands after some tweaking:

    // Page Layout for Perfect Pint
    
    // Most Recent Episode
    
    function perfectPintPageTop() {
    //The Query
    $args = array(
    	'post_type' => 'post',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'show',
    			'field' => 'slug',
    			'terms' => 'perfect-pint'
    		)
    	)
    );
    
    $query = new WP_Query( 'posts_per_page=1', $args );
    
    // The Loop
    if ( $query->have_posts() ) {
    	while ( $query->have_posts() ) {
    		$query->the_post();
    		echo '<li><h2><a href="'.get_permalink().'">' . get_the_title() . '</a></h2></li>';
    		?><div class="entry"><?php the_content(); ?></div> <?php
    	}
    } else {
    	// no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    
    }
    add_action('thematic_abovecontent','perfectPintPageTop');
    
    // Next Four Episodes
    
    function perfectPintPage4() {
    	?><div id="episodes"><ul><?php
    //The Query
    $args2 = array(
    	'post_type' => 'post',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'show',
    			'field' => 'slug',
    			'terms' => 'perfect-pint'
    		)
    	)
    );
    
    $query2 = new WP_Query( array( 'posts_per_page' => 4, 'offset' => 1 ),$args2 );
    
    // The 2nd Loop
    while( $query2->have_posts() ) {
    	$query2->next_post();
    	echo '<li><h2><a href="'.get_permalink( $query2->post->ID ).'">' . get_the_title( $query2->post->ID ) . '</a></h2></li><div class="entry">' . the_content( $query2->post->ID ) . '</div>';
    
    }
    
    // Restore original Post Data
    wp_reset_postdata();
    ?></ul></div><?php
    }
    add_action('thematic_belowcontent','perfectPintPage4');
  2. erikas_boy
    Member
    Posted 1 year ago #

    SOLVED! In the second loop, I needed to append the line:

    while( $query2->have_posts() )

    with:

    : $query2->the_post(); {

    then add

    endwhile;

    before

    wp_reset_postdata();

    Final (working) code looks like this:

    // Page Layout for Perfect Pint
    
    // Most Recent Episode
    
    function perfectPintPageTop() {
    //The Query
    $args = array(
    	'post_type' => 'post',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'show',
    			'field' => 'slug',
    			'terms' => 'perfect-pint'
    		)
    	)
    );
    
    $query = new WP_Query( 'posts_per_page=1', $args );
    
    // The Loop
    if ( $query->have_posts() ) {
    	while ( $query->have_posts() ) {
    		$query->the_post();
    		echo '<li><h2><a href="'.get_permalink().'">' . get_the_title() . '</a></h2></li>';
    		?><div class="entry"><?php the_content(); ?></div> <?php
    	}
    } else {
    	// no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    
    }
    add_action('thematic_abovecontent','perfectPintPageTop');
    
    // Next Four Episodes
    
    function perfectPintPage4() {
    	?><div id="episodes"><ul><?php
    //The Query
    $args2 = array(
    	'post_type' => 'post',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'show',
    			'field' => 'slug',
    			'terms' => 'perfect-pint'
    		)
    	)
    );
    
    $query2 = new WP_Query( array( 'posts_per_page' => 4, 'offset' => 1 ),$args2 );
    
    // The 2nd Loop
    while( $query2->have_posts() ) : $query2->the_post(); {
    	$query2->next_post();
    	echo '<li><h2><a href="'.get_permalink( $query2->post->ID ).'">' . get_the_title( $query2->post->ID ) . '</a></h2></li><div class="entry">' . get_the_content( $query2->post->ID ) . '</div>';
    
    }
    endwhile;
    // Restore original Post Data
    wp_reset_postdata();
    ?></ul></div><?php
    }
    add_action('thematic_belowcontent','perfectPintPage4');

Topic Closed

This topic has been closed to new replies.

About this Topic