SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    How to show first 5 post in a loop then break it and continue again

    I did some research. It turns out to have something like $postcount = 0; and put $postcount++ in the loop. But I can't get it working.

    Say, I have 4 loops on the page.

    The top loop is for a featured category. I have no problem with this loop.

    The second loop is what I'm talking about. It's the main loop (two columns theme) and would show all the posts in descending order. I want to show 5 posts and put a break to it to display ads or some other things then resume to the loop and continue all the way to the last post.

    The third and fourth loops are on the sidebar which show popular and video posts respectively.

    How do I achieve this?

    Thank you,

  2. #2
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Here's what I'm trying to do. Why it's not working:
    Code:
    <?php
    	$postcount = 0;
    	if ( have_posts() ) :
    		while ( have_posts() ) : the_post();
    			if ( $postcount == 0 && $postcount <= 5 ) {
    ?>
    				<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    				<?php 
    					the_excerpt();
    					$postcount ++;
    				?>
    <?php 		}
    		endwhile;
    	endif;
    ?>
    Thanks

  3. #3
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    OK, do something like this:
    Code:
    <?php
    	$postcount = 0;
    	if ( have_posts() ) :
    		while ( have_posts() ) : the_post();
    ?>
    		<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    		<?php 
    			the_excerpt();
    			$postcount ++;	
    			if ( $postcount == 5 ) {
    				echo "Your ad here";
    			}
    		endwhile;
    	endif;
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •