WordPress.org

Ready to get started?Download WordPress

Forums

Style most recent post differently (5 posts)

  1. warpdesign
    Member
    Posted 7 years ago #

    On my homepage, I would like to apply a different style to the most recent post than the others. For example the most recent post would have a white background and everything else would be gray. How can I do this? Would I have to run multiple loops? Or can I add something to the div that would be unique to the most recent post?

  2. Kafkaesqui

    Posted 7 years ago #

  3. zota
    Member
    Posted 6 years ago #

    Could someone please link to the solution to this issue?

    I've been searching the forums for a while now and almost every thread ends with a link to search the forums which leads to results which end with links to search the forums...

    I realize this is a common request, but right now I'm stuck in an infinite forum search loop looking for the common answer.

  4. jbiesnecker
    Member
    Posted 6 years ago #

    There are probably many ways to do this, but this is how I would do it.

    Outside the loop, set a PHP variable like this:

    $is_first_post = 1;

    Then, in a div wrapping your post block, I'd have something like this:

    <div class="post <? if ($is_first_post) { echo "first_post"; } ?>">

    Finally, at the bottom of the loop (but still inside!), a statement like this:

    $is_first_post = 0;

    That would add the "first_post" class to the div on the first loop through (since the variable is set as 1 (you could use 'true', too)), but then after the loop cycles once the variable is set to 0 and never evaluates as true again.

    HTH

  5. zota
    Member
    Posted 6 years ago #

    Thanks, jbiesnecker. I put something together based on your advice combined with information from this thread:
    http://wordpress.org/support/topic/97946

    Set a variable to start counting at the start of the loop:

    <?php while (have_posts()) : the_post();?>
    	<?php $postvariable++; /* count the posts */ ?>

    Make a conditional check within the loop:

    <!-- If first post, show full contents. If not first, show excerpt -->
    <?php if ($postvariable == 1)
    	the_content();
    else
    	the_excerpt(); ?>

    This seems to be working for me, but any further advice or refinements would be appreciated.

Topic Closed

This topic has been closed to new replies.

About this Topic