• Hi,

    Currently I’ve got the following..

    <ul id="index-wall">
    	<?php $posts = get_posts('numberposts=15&order=ASC');
     foreach( $posts as $post ) : ?>
    
    		<li class="boxgrid slidedown">
    			<img class="cover wp-post-image" alt="<?php the_title(); ?>"  src="<?php echo p75GetThumbnail($post->ID, null, null); ?>" style="float:left;margin:0 10px 0 0;" border="0" />
    
    			<p><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><span class="title"><?php the_title(); ?></span><?php the_category(', '); ?></a></p>
    		</li>
    
    	<?php endforeach; ?>
    </ul>

    What I want is using for each is to display 15 posts.. like it does currently but I want to add that when the total number of posts exceeds 15 posts that it creates a new unordered list and then populate it with the next 15 posts until the total number of posts peters out..

    Anyone got any advice?

Viewing 4 replies - 1 through 4 (of 4 total)
  • <ul id="index-wall">
    	<?php
    $counter=1; //create and initialize a counter
     $posts = get_posts('numberposts=-1&order=ASC');
     foreach( $posts as $post ) : ?>
    
    <?php if($counter==15) { //check if the counter reaches a limit, close the unordered list and start a new
    echo '</ul><ul class="index-wall">'; $counter=0; }
    $counter++; //increase counter ?>
    
    		<li class="boxgrid slidedown">
    			<img class="cover wp-post-image" alt="<?php the_title(); ?>" src="<?php echo p75GetThumbnail($post->ID, null, null); ?>" style="float:left;margin:0 10px 0 0;" border="0" />
    			<p><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><span class="title"><?php the_title(); ?></span><?php the_category(', '); ?></a></p>
    		</li>
    	<?php endforeach; ?>
    	</ul>

    untested;
    is using a counter to end and start the unordered lists;
    set ‘numberposts=-1’ to show all posts;
    used a css class .index-wall on the follow-up unordered list for validation reasons;

    Thread Starter masok88

    (@masok88)

    Great, I’ll work on integrating this and let you know how it goes, thanks so much!

    Thread Starter masok88

    (@masok88)

    I’ve got this working now and the only thing I noticed was that it’d display 14 posts before starting a new list, this wasn’t an issue however.

    Is there a way to say..

    For every child (every page composed of 15 entries) echo the following..

    I’m guessing I’d make a counter to divide total posts by 15 and have an echo which looped for every 15 entries?

    it is this code here:
    if($counter==15)

    (where i was not too sure about myself, if it would show the right number of posts, before making a new unordered list);

    try and change it to:
    if($counter > 15)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Display posts using 'foreach' but presented in brackets/units/page’ is closed to new replies.