WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Paging with multiple loops in index (20 posts)

  1. laemo
    Member
    Posted 4 years ago #

    Hello,

    I am trying to use multiple loops within my main index template to show first 5 posts as normal, and only show the titles and post information for another 15 posts.

    The code I am using is as below:

    <?php get_header(); ?>
    	<div id="main" class="clearfix">
    		<div id="posts" class="grid_8">
    			<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    			<?php query_posts('showposts=5'); ?>
    			<?php $posts = get_posts('numberposts=5&offset=0'); foreach ($posts as $post) : start_wp(); ?>
    			<?php static $count1 = 0; if ($count1 == "5") { break; } else { ?>
    			<div class="post">
    				<div class="post-image">
    					<?php if ( function_exists('has_post_thumbnail') && has_post_thumbnail() ) : ?>
    						<?php $src = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'large', false, '' ); ?>
    						<img src="<?php bloginfo( 'template_directory' ); ?>/timthumb.php?src=<?php echo $src[0]; ?>&w=576&h=172&zc=1" border="0" alt="<?php the_title(); ?>" />
    					<?php else : ?>
    						<?php if( get_post_meta( $post->ID, "image_value", true ) ) : ?>
    							<img src="<?php bloginfo( 'template_directory' ); ?>/timthumb.php?src=<?php echo get_post_meta( $post->ID, "image_value", true ); ?>&w=576&h=172&zc=1" border="0" alt="<?php the_title(); ?>" />
    						<?php else : ?>
    							<img src="<?php bloginfo( 'template_directory' ); ?>/timthumb.php?src=<?php echo wp_catch_first_image('l'); ?>&w=576&h=172&zc=1" border="0" alt="<?php the_title(); ?>" />
    						<?php endif; ?>
    					<?php endif; ?>
    				</div>
    				<h1 class="post-title"><a href="<?php the_permalink(); ?>" title="Continue reading &quot;<?php the_title(); ?>&quot;"><?php the_title(); ?></a></h1>
    				<div class="post-meta clearfix">
    					<span class="datetime"><?php the_time('F jS, Y') ?></span>
    					<span class="category"><?php the_category(', ') ?></span>
    					<span class="comment"><?php comments_number('No Comment','1 Comment','% Comments'); ?></span>
    				</div>
    				<div class="post-excerpt"><?php wp_limit_post(200,'.'); ?></div>
    				<span class="post-link"><a href="<?php the_permalink(); ?>" title="Read more about &quot;<?php the_title(); ?>&quot;">Read more &raquo;</a></span>
    			</div>
    
    			<?php $count1++; } ?>
    			<?php endforeach; ?>
    
    			<?php endwhile; endif; ?>
    
    			// Begin posts as list
    
    			<div class="post-small">
    			<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    			<?php query_posts('showposts=15'); ?>
    			<?php $posts = get_posts('numberposts=15&offset=5'); foreach ($posts as $post) : start_wp(); ?>
    			<?php static $count2 = 0; if ($count2 == "15") { break; } else { ?>
    
    			<?php the_date('', '<br />', '<hr />'); ?>
    
    				<h1 class="post-title-small"><a href="<?php the_permalink(); ?>" title="Continue reading &quot;<?php the_title(); ?>&quot;"><?php the_title(); ?></a></h1>
    				<div class="post-meta clearfix">
    					<span class="datetime"><?php if(!function_exists('how_long_ago')){the_time('F jS, Y'); } else { echo how_long_ago(get_the_time('U')); } ?>
    </span>
    					<span class="category"><?php the_category(', ') ?></span>
    					<span class="comment"><?php comments_number('No Comment','1 Comment','% Comments'); ?></span>
    				</div>
    
    			<?php $count2++; } ?>
    			<?php endforeach; ?>
    		</div>
    			<?php endwhile; endif; ?>
    
    			<div class="post-navigation clearfix"><?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?></div>
    		</div>
    		<div id="sidebar" class="grid_4">
    			<?php get_sidebar(); ?>
    		</div>
    	</div>
    <?php get_footer(); ?>

    The problem is, when I try to visit page 2 or any other page that displays the posts, it shows me the exact same posts. I am at my wits end to fix this, would really appreciate if someone could help me figure this out.

  2. laemo
    Member
    Posted 4 years ago #

    Updated, okay I was able to get the first loop working by using the following code:

    <?php get_header(); ?>
    <?php global $query_string; ?>
    	<div id="main" class="clearfix">
    		<div id="posts" class="grid_8">
    			<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
    			<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    			<?php query_posts($query_string.'&showposts=5&paged='.$paged); ?>
    			<?php $posts = get_posts($query_string.'&showposts=5&paged='.$paged); foreach ($posts as $post) : start_wp(); ?>
    			<?php static $count1 = 0; if ($count1 == "5") { break; } else { ?>
    
    			<div class="post">
    				<div class="post-image">
    					<?php if ( function_exists('has_post_thumbnail') && has_post_thumbnail() ) : ?>
    						<?php $src = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'large', false, '' ); ?>
    						<img src="<?php bloginfo( 'template_directory' ); ?>/timthumb.php?src=<?php echo $src[0]; ?>&w=576&h=172&zc=1" border="0" alt="<?php the_title(); ?>" />
    					<?php else : ?>
    						<?php if( get_post_meta( $post->ID, "image", true ) ) : ?>
    							<img src="<?php bloginfo( 'template_directory' ); ?>/timthumb.php?src=<?php echo get_post_meta( $post->ID, "image", true ); ?>&w=576&h=172&zc=1" border="0" alt="<?php the_title(); ?>" />
    						<?php else : ?>
    							<img src="<?php bloginfo( 'template_directory' ); ?>/timthumb.php?src=<?php echo wp_catch_first_image('l'); ?>&w=576&h=172&zc=1" border="0" alt="<?php the_title(); ?>" />
    						<?php endif; ?>
    					<?php endif; ?>
    				</div>
    				<h1 class="post-title"><a href="<?php the_permalink(); ?>" title="Continue reading &quot;<?php the_title(); ?>&quot;"><?php the_title(); ?></a></h1>
    				<div class="post-meta clearfix">
    					<span class="datetime"><?php the_time('F jS, Y') ?></span>
    					<span class="category"><?php the_category(', ') ?></span>
    					<span class="comment"><?php comments_number('No Comment','1 Comment','% Comments'); ?></span>
    				</div>
    				<div class="post-excerpt"><?php wp_limit_post(200,'.'); ?></div>
    				<span class="post-link"><a href="<?php the_permalink(); ?>" title="Read more about &quot;<?php the_title(); ?>&quot;">Read more &raquo;</a></span>
    			</div>
    
    			<?php $count1++; } ?>
    			<?php endforeach; ?>
    
    			<?php endwhile; endif; ?>
    
    			<div class="post-small">
    			<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    			<?php query_posts($query_string.'&showposts=15&paged='.$paged); ?>
    			<?php $posts = get_posts($query_string.'&showposts=15&paged='.$paged.'&offset=5'); foreach ($posts as $post) : start_wp(); ?>
    			<?php static $count2 = 0; if ($count2 == "15") { break; } else { ?>
    
    			<?php the_date('', '<br />', '<hr />'); ?>
    
    				<h1 class="post-title-small"><a href="<?php the_permalink(); ?>" title="Continue reading &quot;<?php the_title(); ?>&quot;"><?php the_title(); ?></a></h1>
    				<div class="post-meta clearfix">
    					<span class="datetime"><?php if(!function_exists('how_long_ago')){the_time('F jS, Y'); } else { echo how_long_ago(get_the_time('U')); } ?>
    </span>
    					<span class="category"><?php the_category(', ') ?></span>
    					<span class="comment"><?php comments_number('No Comment','1 Comment','% Comments'); ?></span>
    				</div>
    
    			<?php $count2++; } ?>
    			<?php endforeach; ?>
    		</div>
    			<?php endwhile; endif; ?>
    
    			<div class="post-navigation clearfix"><?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?></div>
    		</div>
    		<div id="sidebar" class="grid_4">
    			<?php get_sidebar(); ?>
    		</div>
    	</div>
    <?php get_footer(); ?>

    However, no matter what I try, the second loop isn't paging correctly. If i remove the offset option, then it pages correctly, however there is duplication of posts. How do I avoid this?

  3. Mark / t31os
    Moderator
    Posted 4 years ago #

    Paging sets the offset, you can use the paging option or the offset, not both, since they essentially control the same thing...

  4. laemo
    Member
    Posted 4 years ago #

    Here is the problem:

    Using the code above.. it shows me 5 posts in full

    Post 1
    Post 2
    Post 3
    Post 4
    Post 5

    Now, the second loop, without offset shows me the titles of the posts:

    Title 1
    Title 2
    ..
    Title 15

    There is duplication of first 5 titles as the same posts are displayed using the first loop.

    If I USE the offset option, the first page displays correctly (excludes the 5 posts displayed on top). However, on other pages, it shows the exact same posts as shown on the first page.

    Any thoughts how I could fix it? The second loop has to exclude the first 5 posts displayed on that particular page.

  5. Mark / t31os
    Moderator
    Posted 4 years ago #

    Please see the examples given here on using multiple loops.
    http://codex.wordpress.org/The_Loop#Multiple_Loops

  6. laemo
    Member
    Posted 4 years ago #

    The reason I asked for help was because I wasn't able to figure it out. That's the reason why they have the support forums, right?

    Anyway, thanks for trying to help.

  7. Mark / t31os
    Moderator
    Posted 4 years ago #

    You don't need both get_posts and query_posts ...

    You also don't need more then one loop, what you have above could be done with one query, using rewind_posts or using a counter ... since you're essentially just changing elements after a fixed amount of posts..

    Have another read of the examples in writing loops and start again.

    I didn't mean to be unhelpful, but there's numerous areas that need correcting in your above code, and rather then simply say that outright(which you may have considered rude), i thought it would be more polite of me to simply point you back at the examples, which would be along the same lines as any example i would have provided anyway.

    What you may need here is a simple loop with a counter to change elements after a set amount, i know i've given examples of doing just that at least a handful of times before, if you like i can dig up some threads with examples.. does that interest you?

  8. laemo
    Member
    Posted 4 years ago #

    I'd very much appreciate that, thanks :)

    I did speak to a friend who said I can just use one loop and instead of breaking enter the code there, I could definitely use some examples.

  9. Mark / t31os
    Moderator
    Posted 4 years ago #

    I think it may be easier if i write a simple example, it's not very easy finding topics on here using the profile page.

    So i'm clear on your intentions, you want the first 5 posts styled one way, then the following 15 posts styled another ... i can't see that you're trying to do anything more then that, is that correct, so i'm clear?

    First 5 posts inside the..

    <div class="post">

    ..then those that follow (15 to follow) use ...

    <div class="post-small">

    ...etc...

    Yes? :)

  10. laemo
    Member
    Posted 4 years ago #

    Yes, that's correct.
    What I am trying to accomplish is show the first 5 posts with images and excerpt (controlled via a plugin), and just the headline of the next 15.

    Thanks, I really do appreciate you helping out.

  11. Mark / t31os
    Moderator
    Posted 4 years ago #

    Here you go, all you need to do is copy the relevant pieces of code (from what you posted before) into the relevant places i've marked with html comments..

    The bits that look like..

    <!-- text -->

    http://wordpress.pastebin.ca/1804858

    I removed most of your code to make the code easier to understand, i've put quite a few comments in there, hopefully it will make a bit more sense when you look at it..

  12. laemo
    Member
    Posted 4 years ago #

    Believe it or not t31os_, it executed the code, showed me the same post on the entire page for some reason.

    It showed the entire post 5 times first, and then 19439 times again showing just the title (I kid you not!) before throwing this error:
    Fatal error: Maximum execution time of 60 seconds exceeded in C:\wamp\www\wordpress\wp-includes\post.php on line 812

    I tried it both on my local as well as the actual server to confirm this.

    I have updated the pastebin with the code I've used:

    http://wordpress.pastebin.ca/1804858

    Really appreciate your help till here. Logically what you did makes sense, but for some reason it keeps showing just 1 post on the entire page.

  13. Mark / t31os
    Moderator
    Posted 4 years ago #

    Yes apologies, change this line..

    while( have_posts() ) :

    ..to..

    while( have_posts() ) : the_post();
  14. laemo
    Member
    Posted 4 years ago #

    That worked like a charm. Thank you so much :)

    I really really appreciate all the help you've provided.

  15. Mark / t31os
    Moderator
    Posted 4 years ago #

    You're welcome.. :)

  16. laemo
    Member
    Posted 4 years ago #

    t31os_, if I can bother you with just one idea. Is it possible to run the loop to show posts from say a particular date in full (most recent day) and style the rest of them differently?

    I'd like to show just today's posts in full and the rest in a list. I know I have already bothered you enough, and I have done a bit of searching but cannot seem to find any success.

  17. Mark / t31os
    Moderator
    Posted 4 years ago #

    laemo please start a new thread with a detailed description of what you wish to do.

    I don't feel i know the answer to that particular question, and i don't think the question will get noticed under this thread's heading.

    I'd happily test some code, but my local install is down right now (new configuration i'm still working on - multiple virtual hosts etc..), so until then i'd suggest a fresh topic where you can invite others to offer suggestions.

  18. laemo
    Member
    Posted 4 years ago #

    Okay, thanks again. Starting a new thread. :)

  19. smoothman
    Member
    Posted 3 years ago #

    Is it possible to upload the code again? I am searching for the exact same thing for 2 days now... All the codes I find & test are either offset or navigation nothing works together....

    Thanx in advance

  20. VikingBrent
    Member
    Posted 3 years ago #

    Ditto smoothman, I'm trying to accomplish nearly the exact same thing and would love to take a look at your code.

    Thank you in advance

Topic Closed

This topic has been closed to new replies.

About this Topic