WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Pagination with Multiple loops (20 posts)

  1. adamccc
    Member
    Posted 2 years ago #

    Hi there,

    I've been trying to come up with multiple loops for my site with displays posts as such -

    1 post
    2 posts
    4 posts
    3 posts
    3 posts

    To work with the redesign of our website (http://hustv.co.uk). I've managed to achieve this following this blog (http://perishablepress.com/multiple-loops-and-multiple-columns-with-wordpress/) but as I'm sure you'll guess pagination doesn't work using this method

    After lots of searching I managed to find this post that looks like someone got it to work http://wordpress.org/support/topic/paging-with-multiple-loops-in-index

    But all the code that was used was in pastebin and has now expired :( can Mark / t31os or anyone provide some help?

    <div class="top_story">
    	<?php _posts('showposts=1'); ?>
    	<?php $posts = get_posts('numberposts=1&offset=0'); foreach ($posts as $post) : start_wp(); ?>
    	<?php static $count1 = 0; if ($count1 == "1") { break; } else { ?>
            <header class="entry-header">
    		<?php $youtubeid = get_post_meta($post->ID, "youtubeid", true); ?>
    		<?php if($youtubeid !== '') { ?>
    			<iframe width="747" height="419" src="http://www.youtube.com/embed/<?php echo $youtubeid; ?>" frameborder="0" allowfullscreen></iframe>
    		<?php } ?>
    		<?php if($youtubeid == '') { ?>
    			<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php if ( has_post_thumbnail() ) {
    			the_post_thumbnail(747,419);
    		} ?><a/>
    		<?php } ?>
    		<?php $cat = get_the_category(); ?>
    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><h3 class="<?php echo $cat[0]->slug;?>"><?php the_title(); ?></h3></a>
    <?php unset($cat); ?>
    		</header><!-- .entry-header -->
    	<?php if ( is_search() ) : // Only display Excerpts for Search ?>
    		<div class="entry-summary">
    			<?php the_excerpt(); ?>
    		</div><!-- .entry-summary -->
    		<?php else : ?>
    		<div class="entry-content">
    			<?php the_excerpt( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyeleven' ) ); ?>
    			<?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'twentyeleven' ) . '</span>', 'after' => '</div>' ) ); ?>
    		</div><!-- .entry-content -->
    		<?php endif; ?>
    			<?php $count1++; } ?>
    			<?php endforeach; ?>
    		</div>
    		<div class="linebreak"></div>
    
    				<div class="row-fluid">
    				<?php get_posts('showposts=2'); ?>
    				<?php $posts = get_posts('numberposts=2&offset=1'); foreach ($posts as $post) : start_wp(); ?>
    				<?php static $count2 = 0; if ($count2 == "2") { break; } else { ?>
    <div class="span6 split_story">
    			<header class="entry-header">
    
    			<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php if ( has_post_thumbnail() ) {
    	the_post_thumbnail(747,419);
    } ?><a/>
    
    			<?php $cat = get_the_category(); ?>
    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><h3 class="<?php echo $cat[0]->slug;?>"><?php the_title(); ?></h3></a>
    <?php unset($cat); ?>
    		</header><!-- .entry-header -->
    
    		<?php if ( is_search() ) : // Only display Excerpts for Search ?>
    		<div class="entry-summary">
    			<?php the_excerpt(); ?>
    		</div><!-- .entry-summary -->
    		<?php else : ?>
    		<div class="entry-content">
    			<?php the_excerpt( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyeleven' ) ); ?>
    			<?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'twentyeleven' ) . '</span>', 'after' => '</div>' ) ); ?>
    		</div><!-- .entry-content -->
    		<?php endif; ?>
    		</div>
    				<?php $count2++; } ?>
    				<?php endforeach; ?>
    
    						<div class="linebreak"></div>
    					</div>
    
    				<?php query_posts('showposts=4'); ?>
    				<?php $posts = get_posts('numberposts=4&offset=3'); foreach ($posts as $post) : start_wp(); ?>
    				<?php static $count3 = 0; if ($count3 == "4") { break; } else { ?>
    
    <div class="row-fluid row_story">
    
    						<div class="span4">
    			<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php if ( has_post_thumbnail() ) {
    	the_post_thumbnail(747,419);
    } ?><a/>
    						</div>
    						<div class="span8">
    								<header class="entry-header">
    			<?php $cat = get_the_category(); ?>
    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><h3 class="<?php echo $cat[0]->slug;?>"><?php the_title(); ?></h3></a>
    <?php unset($cat); ?>
    
    		</header><!-- .entry-header -->
    
    		<?php if ( is_search() ) : // Only display Excerpts for Search ?>
    		<div class="entry-summary">
    			<?php the_excerpt(); ?>
    		</div><!-- .entry-summary -->
    		<?php else : ?>
    		<div class="entry-content">
    			<?php the_excerpt( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyeleven' ) ); ?>
    			<?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'twentyeleven' ) . '</span>', 'after' => '</div>' ) ); ?>
    		</div><!-- .entry-content -->
    		<?php endif; ?>
    						</div>
    						<div class="linebreak"></div>
    					</div>
    
    						<?php $count3++; } ?>
    				<?php endforeach; ?>
    
    					<div class="row-fluid triple_story">
    
    				<?php query_posts('showposts=3'); ?>
    				<?php $posts = get_posts('numberposts=3&offset=7'); foreach ($posts as $post) : start_wp(); ?>
    				<?php static $count3 = 0; if ($count4 == "3") { break; } else { ?>
    
    						<div class="span4">
    
    								<header class="entry-header">
    
    			<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php if ( has_post_thumbnail() ) {
    	the_post_thumbnail(747,419);
    } ?><a/>
    
    			<?php $cat = get_the_category(); ?>
    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><h3 class="<?php echo $cat[0]->slug;?>"><?php the_title(); ?></h3></a>
    <?php unset($cat); ?>
    
    		</header><!-- .entry-header -->
    
    		<?php if ( is_search() ) : // Only display Excerpts for Search ?>
    		<div class="entry-summary">
    			<?php the_excerpt(); ?>
    		</div><!-- .entry-summary -->
    		<?php else : ?>
    		<div class="entry-content">
    			<?php the_excerpt( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyeleven' ) ); ?>
    			<?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'twentyeleven' ) . '</span>', 'after' => '</div>' ) ); ?>
    		</div><!-- .entry-content -->
    		<?php endif; ?>
    
    						</div>
    
    				<?php $count4++; } ?>
    				<?php endforeach; ?>
    					<div class="linebreak"></div>
    
    					<div class="row-fluid triple_story">
    
    				<?php query_posts('showposts=3'); ?>
    				<?php $posts = get_posts('numberposts=3&offset=10'); foreach ($posts as $post) : start_wp(); ?>
    				<?php static $count3 = 0; if ($count5 == "3") { break; } else { ?>
    
    						<div class="span4">
    
    								<header class="entry-header">
    
    			<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php if ( has_post_thumbnail() ) {
    	the_post_thumbnail(747,419);
    } ?><a/>
    
    			<?php $cat = get_the_category(); ?>
    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><h3 class="<?php echo $cat[0]->slug;?>"><?php the_title(); ?></h3></a>
    <?php unset($cat); ?>
    		</header><!-- .entry-header -->
    
    		<?php if ( is_search() ) : // Only display Excerpts for Search ?>
    		<div class="entry-summary">
    			<?php the_excerpt(); ?>
    		</div><!-- .entry-summary -->
    		<?php else : ?>
    		<div class="entry-content">
    			<?php the_excerpt( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyeleven' ) ); ?>
    			<?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'twentyeleven' ) . '</span>', 'after' => '</div>' ) ); ?>
    		</div><!-- .entry-content -->
    		<?php endif; ?>
    
    						</div>
    
    				<?php $count5++; } ?>
    				<?php endforeach; ?>
    
     	</div>

    Thanks

  2. adamccc
    Member
    Posted 2 years ago #

    Oops website is http://hushtv.co.uk

  3. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it with a counter on one loop. Here is a simplified example:

    <?php if ( have_posts() ) : ?>
    	<?php while ( have_posts() ) : the_post(); ?>
    <?php
    // first post
    $post_count = $wp_query->current_post+1;
    if($post_count == 1){
    	echo '<div id="first-post">';
    }
    if($post_count == 2 || $post_count == 4 || $post_count == 8 || $post_count == 11){
      // close the last column and start a new one
      echo '</div><!-- closing '.$post_count.' -->';
    	echo '<div id="column-'.$post_count.'">';
    }
    ?>
    <!-- normal loop stuff -->
    <?php get_template_part( 'content', get_post_format() ); ?>
    
    <?php
    if($post_count == $wp_query->post_count){
      // close the last column on the page
    	echo '</div><!-- closing last column-->';
    }
    ?>
    <?php endwhile; ?>
    <?php else : ?>
    <h1 class="entry-title">Nothing Found</h1>
    <?php endif; ?>

    This will divide your posts into divs with id's:
    first-post, column-2, column-4, column-8 and column-11

  4. adamccc
    Member
    Posted 2 years ago #

    Thanks for the reply - I don't think this will work for me as I'm not styling the posts with CSS but putting them in a html structure so just having id's on everything won't work.

    Someone got it work here in the same way
    http://wordpress.org/support/topic/paging-with-multiple-loops-in-index

  5. adamccc
    Member
    Posted 2 years ago #

    @keesiemeijer is it possible for me to split the if($post_count == 2/4/8) out and have each if statement with a different div layout?

  6. keesiemeijer
    moderator
    Posted 2 years ago #

    The problem is that you can only do pagination for one loop. You could get the different templates with get_template_part: http://codex.wordpress.org/Function_Reference/get_template_part

    <?php if ( have_posts() ) : ?>
    <?php $temp_content=''; ?>
    	<?php while ( have_posts() ) : the_post(); ?>
    <?php
    // first post
    $post_count = $wp_query->current_post+1;
    if($post_count == 1){
    	$content_template = get_post_format();
    	echo '<div id="first-post">';
    }
    if($post_count == 2 || $post_count == 4 || $post_count == 8 || $post_count == 11){
      $content_template = $post_count;
      // close the last column and start a new one
      echo '</div><!-- closing '.$post_count.' -->';
    	echo '<div id="column-'.$post_count.'">';
    }
    ?>
    <!-- normal loop stuff -->
    <?php get_template_part( 'content', $content_template );  ?>
    
    <?php
    if($post_count == $wp_query->post_count){
      // close the last column on the page
    	echo '</div><!-- closing last column-->';
    }
    ?>
    <?php endwhile; ?>
    <?php else : ?>
    <h1 class="entry-title">Nothing Found</h1>
    <?php endif; ?>

    You have to create separate theme files for the different blocks of code: content.php, content-2.php, content-4, content-8, content-11

  7. keesiemeijer
    moderator
    Posted 2 years ago #

    @keesiemeijer is it possible for me to split the if($post_count == 2/4/8) out and have each if statement with a different div layout?

    Yes it is possible with this:

    <?php if ( have_posts() ) : ?>
    <?php $temp_count=''; ?>
    	<?php while ( have_posts() ) : the_post(); ?>
    <?php
    // first post
    $post_count = $wp_query->current_post+1;
    if($post_count == 1){
    	echo '<div id="first-post">';
    	// do stuff for first post
    }
    if($post_count == 2 || $post_count == 4 || $post_count == 8 || $post_count == 11){
      $temp_count = $post_count;
      // close the last column and start a new one
      echo '</div><!-- closing '.$post_count.' -->';
    	echo '<div id="column-'.$post_count.'">';
    }
    ?>
    <?php if($temp_count == 2) : ?>
    <!-- do stuff for posts 2 and 3 -->
    <?php endif; ?>
    
    <?php if($temp_count == 4) : ?>
    <!-- do stuff for posts 4 - 7 -->
    <?php endif; ?>
    
    <?php if($temp_count == 8) : ?>
    <!-- do stuff for posts 8 - 10 -->
    <?php endif; ?>
    
    <?php if($temp_count == 11) : ?>
    <!-- do stuff for posts 11 - 13 -->
    <?php endif; ?>
    
    <?php
    if($post_count == $wp_query->post_count){
      // close the last column on the page
    	echo '</div><!-- closing last column-->';
    }
    ?>
    <?php endwhile; ?>
    <?php else : ?>
    <h1 class="entry-title">Nothing Found</h1>
    <?php endif; ?>
  8. adamccc
    Member
    Posted 2 years ago #

    Brilliant, your first example with content template works perfectly!

    Thanks so much!

    Quick last question, how would I filter the posts by category with this method?

    Cheers

  9. keesiemeijer
    moderator
    Posted 2 years ago #

    Filter the whole loop or just one block of posts, for example posts 4 to 7 (trickier)?

  10. adamccc
    Member
    Posted 2 years ago #

    The whole loop

  11. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it with this in your theme's functions.php:

    function my_post_queries( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
        // on the home page
        if(is_home()){
          // set the category ID here
          $query->set('cat', 23);
        }
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    Change the category ID in

    $query->set('cat', 23);
  12. adamccc
    Member
    Posted 2 years ago #

    Should of probably been a bit clearer - need to do this about 10 times on different pages, is there anyway it can be in the loop.

    Although I've just tried using the same loop on a template page (http://www.hushtv.co.uk/tv/) and it just shows a link?

  13. keesiemeijer
    moderator
    Posted 2 years ago #

    You could also try it with this just before the loop:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $the_query = new WP_Query('cat=23&paged='.$paged); ?>

    but you'll have to change the start of the loop.
    change this:

    <?php while ( have_posts() ) : the_post(); ?>

    to this

    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    http://codex.wordpress.org/Function_Reference/WP_Query
    http://codex.wordpress.org/Function_Reference/query_posts

    The code in your functions.php for your home page is faster and better. Read here why: http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/

    But it does not work on custom Page templates. Use new WP_Query for those.

  14. adamccc
    Member
    Posted 2 years ago #

    *Realising i'm sounding like an annoying noob right now* but managed to get it to display posts from the correct category on a template page but:

    -navigation to older posts doesn't display
    -doesn't inherit the styles from content.php, content-4.php etc

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $the_query = new WP_Query('cat=24&paged='.$paged); ?>
    				<?php if ( have_posts() ) : ?>
    				<?php $temp_content=''; ?>
    				<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    				<?php
    				// first post
    				$post_count = $wp_query->current_post+1;
    				if($post_count == 1){
    					$content_template = get_post_format();
    					echo '<div id="top_story">';
    				}
    				if($post_count == 2 || $post_count == 4){
    				  $content_template = $post_count;
    				  // close the last column and start a new one
    				  echo '</div><div class="linebreak"></div>';
    					echo '<div class="row-fluid">';
    				}
    
    				if($post_count == 8){
    					$content_template = $post_count;
    				  // close the last column and start a new one
    				  echo '</div>';
    					echo '<div class="row-fluid">';
    				}
    				?>
    				<!-- normal loop stuff -->
    				<?php get_template_part( 'content', $content_template );  ?>
    
    				<?php
    				if($post_count == $wp_query->post_count){
    				  // close the last column on the page
    					echo '</div><!-- closing last column-->';
    				}
    				?>
    				<?php endwhile; ?>
    				<?php else : ?>
    				<h1 class="entry-title">Nothing Found</h1>
    				<?php endif; ?>
    				<div class="linebreak"></div>
    				<?php twentyeleven_content_nav( 'nav-below' ); ?>
  15. adamccc
    Member
    Posted 2 years ago #

    Thanks for all your help by the way.

  16. keesiemeijer
    moderator
    Posted 2 years ago #

    I don't have time right now. I will look into it tomorrow if someone else doesn't chime in with an answer.

  17. adamccc
    Member
    Posted 2 years ago #

    Okay thanks

  18. keesiemeijer
    moderator
    Posted 2 years ago #

    I think this will work. change this:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $the_query = new WP_Query('cat=24&paged='.$paged); ?>
    				<?php if ( have_posts() ) : ?>
    				<?php $temp_content=''; ?>
    				<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    to this:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $the_query = query_posts('cat=24&paged='.$paged); ?>
    <?php if ( have_posts() ) : ?>
    <?php $temp_content=''; ?>
    <?php while ( have_posts() ) : the_post(); ?>
  19. adamccc
    Member
    Posted 2 years ago #

    Worked perfectly! I think I owe you a beer, what's your Paypal address?

  20. keesiemeijer
    moderator
    Posted 2 years ago #

    No worries, I don't have one. I'm glad you got it resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic