• Resolved RyanJenkins

    (@ryanjenkins)


    I’ve got a mini-loop on my homepage.

    It looks like this:

    <?php
    // Include WordPress
    define('WP_USE_THEMES', false);
    require('./blog/wp-blog-header.php');
    query_posts('showposts=2');
    ?>
    
    [Plenty of homepage content]
    
    <?php while(have_posts()): the_post(); ?>
    <br /><span class="blogtitle"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span>
    <span class="blogdate"><?php the_time('F jS, Y') ?></span>
    <?php the_excerpt(); ?>
    <hr noshade style="margin-top:14px;margin-bottom:-4px;padding:0px;">
    <?php endwhile; ?>

    The Loop goes through two posts and after each one places a horizontal rule, just like it’s told to. This ends up looking silly, though, because after my second post, there is a horizontal rule, and then the very bottom of the website, nothing after it. I’d like to do away with this.

    So, what I could do is something like this:

    <?php while(have_posts()): the_post(); ?>
    <br /><span class="blogtitle"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span>
    <span class="blogdate"><?php the_time('F jS, Y') ?></span>
    <?php the_excerpt(); ?>
    <hr noshade style="margin-top:14px;margin-bottom:-4px;padding:0px;">
    
    [manually increment the Loop to the next post, then paste the same code to display and format the post]
    
    <br /><span class="blogtitle"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span>
    <span class="blogdate"><?php the_time('F jS, Y') ?></span>
    <?php the_excerpt(); ?>
    <?php endwhile; ?>

    Is there a function for this, to skip, continue, increment, or iterate the Loop? Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Here is code for how to do what you want

    In this example, output an <hr> after all posts except the last one

    <?php
    if( ($wp_query->current_post + 1) < ($wp_query->post_count) ) {
      echo("<hr />");
    }  ?>

    (post_count starts at 1 and current_post starts at 0. )

    Thread Starter RyanJenkins

    (@ryanjenkins)

    Fantastic, perfect, thanks a million.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Incrementing the Loop manually’ is closed to new replies.