One Post Loop (11 posts)

  1. Jonathan Dingman
    Posted 11 years ago #

    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?

  2. navid
    Posted 11 years ago #

    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.


  3. Jonathan Dingman
    Posted 11 years ago #

    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?

  4. clay
    Posted 11 years ago #

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

  5. ringmaster
    Posted 11 years ago #

    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.

  6. Jonathan Dingman
    Posted 11 years ago #

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

  7. ringmaster
    Posted 11 years ago #

    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.

  8. Jonathan Dingman
    Posted 11 years ago #

    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.


  9. Orin
    Posted 11 years ago #

    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' .

  10. TechGnome
    Posted 11 years ago #

    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.


  11. Orin
    Posted 11 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic