WordPress.org

Ready to get started?Download WordPress

Forums

Display posts using 'foreach' but presented in brackets/units/page (5 posts)

  1. masok88
    Member
    Posted 3 years ago #

    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?

  2. alchymyth
    Forum Moderator
    Posted 3 years ago #

    <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;

  3. masok88
    Member
    Posted 3 years ago #

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

  4. masok88
    Member
    Posted 3 years ago #

    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?

  5. alchymyth
    Forum Moderator
    Posted 3 years ago #

    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)

Topic Closed

This topic has been closed to new replies.

About this Topic