Welcome, visitor! Log in
 

KB Plugins blog

The best Wordpress plugins are free

How do I make my first post look different from the rest?

WordPress users frequently ask how to make their most recent post look different from the rest. Maybe they want to make the font larger, or the background color lighter, or whatever. With a small sprinkling of PHP, this is easy to do. But first, an overview of what we’re trying to accomplish.

Overview of What We’ll Be Doing

What we’re going to do is add a special CSS class to the first post, but not to the others. In most themes, each post gets wrapped in a div that looks something like this:

<div class="post">...</div>

We’ll leave that alone for all the posts except the first. For the first, we’ll change that to this:

<div class="post firstpost">...</div>

And then it’s a simple matter of defining a background color for the .firstpost class in your style.css, like this:

.firstpost{background:#f00;}

So that’s what we want to do. It’s easy with a small amount of PHP.

How to Do It

Open up your WordPress template folder. You need to open whichever file controls the part of your blog in question. If we’re talking about your main page (we probably are), then you probably want to open index.php. (But if there’s a home.php, open that instead.)

Find the beginning of the loop . Usually, that looks something like one of these:

<?php if ( have_posts() ) : while( have_posts() ): the_post(); ?>
<?php while( have_posts() ): the_post(); ?>
<?php while( have_posts() ){ ?>

Now, just before the loop starts, insert this line of code, exactly as shown, on a line by itself:

<?php $firstClass = 'firstpost'; ?>

Now, go into the loop and find the first <div> after the loop starts. In most themes, it will look something like this:

<div class="post">

The class name might be different, but that’s the general idea. Change it to this:

<div class="post <?php echo $firstClass; ?>">

Easy enough so far, right? There’s one more step we need to take, otherwise every post will get that firstpost class added to it, and we don’t want that. So on the line right after the one you just edited, add this in:

<?php $firstClass = ""; ?>

That’s two quotation marks in a row, just in case it isn’t clear. With that piece of code inserted, you’re all done. For the first post, $firstClass is firstpost, but for later posts, $firstClasswill be blank, so echoing it into the div won’t have any effect.

11 Comments

  1. Posted March 30, 2008 at 4:12 am | Permalink

    Thank you for this easy to understand explanation! =)

  2. Posted July 24, 2008 at 5:05 pm | Permalink

    Wow, this is just great. You saved my day buddy. I’m using your hack to not display a border-top for the first post (so we don’t have double borders).

    Thanks!

  3. Posted February 6, 2009 at 1:22 pm | Permalink

    Thank you for this tutorial, just right when I needed it :)

  4. Posted June 11, 2009 at 8:15 pm | Permalink

    That’s exactly what I was looking for, in a very easy-to-understand explanation.
    Thank-you!

  5. Yup
    Posted June 18, 2009 at 10:36 pm | Permalink

    This totally worked! and so simple. Thanks!

  6. Posted December 31, 2009 at 12:46 am | Permalink

    This was exactly what I’m searching for. Great! Thanks a lot!

  7. Posted May 15, 2010 at 4:59 pm | Permalink

    Cool! thanks a lot! That really works after looking for the solution for a while!

  8. Gary
    Posted June 15, 2010 at 9:46 pm | Permalink

    Is there a way to make every other post have a different class so it can have different font/color/bkg, etc?

  9. Posted June 16, 2010 at 7:42 am | Permalink

    Gary: It’s easiest to help those who help themselves. Poke around a bit.

  10. Gary
    Posted June 16, 2010 at 10:06 am | Permalink

    Adam, thanks for the link. I was poking around but wasn’t getting anywhere so thanks again!

  11. Posted June 18, 2010 at 1:09 am | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comment Guidelines
  • Yes, your comments will be visible to everybody. You can disagree, but civilly.
  • I have the right to delete abusive comments.
  • Allowed HTML: <a> <b> <blockquote> <cite> <code> <em> <i> <strong>
  • Put code in `backticks` (above your "Tab" key) or it won't display well
  • Gravatars: To override the default image by your comment, use a gravatar.
  • Links: If you include more than one link, your comment will go into the spam queue.

Please read before commenting: Because I am now employed and not just a student, I provide only minimal support for my plugins. Sorry.

If you have a bug report, feature request, or other general feedback about a plugin, please leave a comment—but do not expect an immediate response. If you are requesting help, though, please check the plugin's documentation thoroughly rather than ask your question as a comment.

Thank you for your understanding.