WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Last post in a loop (5 posts)

  1. jeffreyvr
    Member
    Posted 3 years ago #

    Hello everyone,

    I'd like to display my latest post in my loop a little different than the rest. I'd like to get to a solution without using post_per_page or showposts.

    This my code:

    <?php query_posts('cat=21'); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>,
    <?php endwhile; else: ?>
    <?php endif; wp_reset_query(); ?>

    As you can see there is a ',' at the end of the link in the loop. In the latest post that is suppost to be '.'.

    Hoping for help.

  2. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    I'd like to get to a solution without using post_per_page or showposts.

    then use $wp_query->post_count; and a counter;

    or:

    <?php query_posts('cat=21'); if ( have_posts() ) : $sep = ''; while ( have_posts() ) : the_post(); ?>
    <?php echo $sep; $sep = ', '; ?><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    <?php endwhile; echo '.'; else: ?>
    <?php endif; wp_reset_query(); ?>
  3. jeffreyvr
    Member
    Posted 3 years ago #

    Thanks for your answer alchymyth. The output is like this now:

    Word , Word2 , Word3 .

    There is a space between both sides of the comma. I would like it to be like this: Word, Word2, Word3.

    I feel a bit silly asking but I can't figure out where that space comes from.

  4. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    the linebreak after
    ...?></a>
    seems to be causing it;

    try and add this section as in one line:
    <?php echo $sep; $sep = ', '; ?><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a><?php endwhile; echo '.'; else: ?>

  5. jeffreyvr
    Member
    Posted 3 years ago #

    Close call. Tried that, but didn't help.

    But... when I putted the whole code on one line it did. :)

    Thanks you very much for your help and your very fast response time!

Topic Closed

This topic has been closed to new replies.

About this Topic