Support » Fixing WordPress » One Post Loop

  • Does anyone know the code to setup a custom ‘first post’ for the loop? I want to be able to apply a class which is specific under style.css to my FIRST post only. Anyone have an idea how to do this?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Set up a counter variable like $counter = 1; outside of the loop. Then, in the loop, do a if( $counter == 1 ) and add the class to the post. Remember, in CSS, you can give tags several classes.

    For example:
    <div id="post434" class="bigborder lightbackground">....</div>

    while you can do:

    #post434 { … }
    .bigborder { … }
    .lightbackground { … }

    I hope this helps.


    I setup a loop like that, but it’s still not working correctly. It shows up under every post, not just the single post. Any other ideas how to setup the single post loop?

    are you incrementing the counter at the end of the loop?

    Change this line in your loop:
    <div class="post">

    To this:
    <div class="post<?php if(!is_single() && $post==$posts[0]) echo ' firstpost';?>">

    Add the appropriate style rules to “.firstpost” in your theme’s stylesheet.

    Ringermaster: Am I still including the $counter = 1; before that or just that single line of code by itself?

    Just that one line. No counter.

    The “if” statement checks to see if the current post is the first post among all the posts it’s going to display. If it is and the post is not the only post (is_single()) then it adds the extra class name.

    Perfect, I’m not sure why it wans’t working the first time. It seems to be working fine now. thank you for giving me that small piece of code. It seems that under 1.5 it makes it so much easier than having to make a loop and figure out where to end the loop, etc, etc. It makes it quite easy with 1.5.


    This didn’t work for me the first time, although it does in fact work perfectly.

    The problem is that php will set the category as ‘post’ plus ‘ firstpost’, and having the class ‘.post firstpost’ doesn’t work as the browser will be looking for a <firstpost> tag. So I changed it to this:

    <div class="post<?php if(!is_single() && $post==$posts[0]) echo '-first';?>">

    Thus, the first post is ‘.post-first’ and every other post is just ‘.post’ .




    1) It’s CLASS, not category.
    2) Having class =”post firstpost” is perfectly valid. It will simply apply the post class CSS then apply the firstpost class CSS. I’ve seen it done before, and have even done it myself a few times. There’ s nothing wrong with it.


    1) You’re correct, wrong word.
    2) Ok, wasn’t quite clear on how this was supposed to work. The important part is it does. My modification is for those that want the classes independant of each other, which was my aim.

    Thanks for clarifying this to everyone reading this thread.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘One Post Loop’ is closed to new replies.