49 | Styling the First Post Different


How to style the first / last / latest / newest post in a WordPress site different?

The default advice usually is to use a counter variable and a conditional statement to check for the first post in the loop; that approach obviously works fine, however requires a few lines of extra code before and in the loop.

A more condensed approach is to use $wp_query->current_post which returns the current post number in the loop, starting with 0 (zero) for the first post.

This can be combined with a check, if the page is really the first page, and not one of the paginated pages, using !is_paged().

If the goal is just to apply different css styles to the first post, it is best to add a unique css class to the post_class() which is used in most recent themes; like so: post_class($extra); to add the extra class to post_class.

All combined might look like (based on the code of content.php in Twenty Eleven):

<article id="post-<?php the_ID(); ?>" <?php $extra = ( $wp_query->current_post == 0 && !is_paged() ) ? 'specialclass' : ''; post_class($extra); ?>>

If the goal is to have a totally different output for the first post, then a conditional structure is needed (within the loop, wrapping the post output):

<?php if( $wp_query->current_post == 0 && !is_paged() ) : ?>
/*the output of the first post*?
<?php else : ?>
/*the output of all other posts*/
<?php endif; ?>
This entry was posted in Easy Coding for Wordpress, WordPress and tagged , , , . Bookmark the permalink.

14 Responses to Styling the First Post Different

  1. Mitchel comments:

    To conclude it is best to ensure that the auto phone mount is made
    from silicone at the suction cup base and has a twisting or turning knob
    that permits you to fasten the mount tightly to your windscreen.
    The five row landscape keyboard of the new Samsung phone is an
    answer to the wishes of many. Social based applications: A large number
    of Smartphone (lintas.me) users these days tweet
    and update their Facebook status through their i – Phones.

  2. Suzette comments:

    Baird, video online games created nearly US $60 billion in 2011.
    Sweet Crush is an exceptionally well known activity now getting performed on cellular products, tablets and
    desktops. The game’s style is lively and there is a not so
    important tale line.

    My webpage; summoners war sky arena hack (kaleicious.tumblr.com)

  3. Tresa comments:

    ‘ Nausea : 14 to 23% of women ‘ Vomiting : About 6% of women ‘ Irregular menstrual bleeding ‘ Itching and a rash ‘ Sudden or unusual cramps or pain in the stomach or
    belly ‘ Migraine or severe headache ‘ Any effects that persist or worsen ‘ If you vomit within one hour of taking plan B, you
    may need to take another dose ‘ Breast tenderness, headache, dizziness, fatigue, lower abdominal
    pain, and diarrhea. The grand key is to realize that there is only one overriding feeling
    we should have for ourselves and others-unconditional
    love. These products were researched, developed and created by the thinning hair experts at B’IOTA Laboratories.

    Review my site :: photoshop gratuit

  4. Madonna comments:

    Pretty component of content. I simply stumbled upon your weblog and in accession capital to assert that I get in fact enjoyed
    account your blog posts. Any way I’ll be subscribing for your feeds and even I achievement you get admission to consistently fast.

    Review my web blog :: Burlington Coat Factory Coupons Printable

  5. Marianne comments:

    As a Green Keeper on a golf course, and landscaper with experience, I’m asked,
    continually, for help with people’s gardens. Fix it: Keep up with routine maintenance and it won’t bury you when you
    are getting a house ready to sell. Chances are you rent or
    lease the property upon which your business is located,
    and really have no idea about the types of trees and shrubs that are planted and why they are planted where they

    my page – landscaping ideas for front of house (toboganium.com)

  6. Katia comments:

    Hi there this is kimd of of off topic but I was wanting to know if
    blogs use WYSIWYG editors oor if you have to manually code with HTML.
    I’m starting a blog soon but have no coding skills so I wanted to get guidance
    from someone with experience. Any help would be greatly appreciated!

    My web site Dragons Of Atlantis Cheatts (https://Www.Facebook.Com/)

  7. Nicki comments:

    Every weekend i used to visit this web page, for the
    reason that i wish for enjoyment, as this this website conations actually nice funny data too.

    My homepage; dummy json data

  8. Esperanza comments:

    You really make it appear really easy with your presentation but I find
    this topic to be really something that I think I would by no means understand.

    It sort of feels too complicated and very vast for me. I’m having a look forward to your
    next put up, I will attempt to get the hang of it!

    Feel free to visit my blog post Gucci Borse Outlet

  9. Tracie comments:

    Finally, she advises: No edit is too small. Bieber has been his now-typical entourage of Louis XV.
    In addition, cocoa butter as one of you reclining on his tour last year had been quite a while
    Orlando Bloom faced off with a touch more sweetthan Hiris, despite the similar.

    You’ll be cheering yourself if you only made $1 royalty.
    Her favorite scent may no longer have to go into them can ever perfume
    justin bieber girlfriend go back.

    Also visit my site; justin bieber the key gift set

  10. KIDO comments:

    but what if i want to last post?

    • michael replies:

      the ‘last’ post on a page in the loop is:
      if( $wp_query->current_post == $wp_query->post_count-1 ) { /*do whatever is needed*/ }

  11. kamil comments:

    but what if I want not just first but first and second?

    • michael replies:

      you change the conditional statement; example for first and second post:
      <?php if( $wp_query->current_post <= 1 && !is_paged() ) : ?>

  12. Matt comments:

    Great post. Exactly what I was looking for after lots of digging. Thanks!

Leave a Reply

Your email address will not be published.


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>