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.

22 Responses to Styling the First Post Different

  1. Shayla comments:

    After one starts utilizing this hack, you don’t need to worry
    in regards to the variety of tokens you might have in your chaturbate account to observe
    your favorite cam show from your favorite model.

    my blog post play mobile games on pc

  2. Candice comments:

    The elite Modular Storage space System is New as well as exclusive to The wardrobe design perth; Vernon, Guy, modular but custom made to your demands.

  3. Antoine comments:

    If you get the right topper, even if it is constructed from best adjustable memory foam mattress brands foam, after that it will
    certainly assist to safeguard your bed mattress from dirt, dander, and also other debris.

  4. Leonora comments:

    Here at Wardrobe Fashion we comprehend you could not try garments
    on prior to buying, we enjoy to offer you a reimbursement or credit rating,
    must you publish your garment/s back to Head Office in Western wardrobes perth australia (Jeana) within 14
    days of the date of delivery to you.

  5. Marie comments:

    It was intend great post to read (Harrison) have a jes maharry charm with it and also there was
    not one with either of guides I bought.

  6. Calvin comments:

    Manhattan’s Magoosh gmat review – Ned
    – Uncovered” eBook -Consists of a substantial conversation of GMAT technique in addition to technique inquiries and explainations.

  7. Marcelino comments:

    I am utilizing Magoosh Gre review (http://www.mjzcnd.com/home.php?mod=space&uid=16366&do=profile&from=space)
    myself as well as I am struggling with the difficult problem concerns of the quant

  8. Dessie comments:

    Selon nos informations, Mario Balotelli va s’engager avec l’OGC Nice ligue 1 en direct streaming provenance de
    Liverpool dans le cadre d’un transfert sec sans indemnités!

  9. Margarito comments:

    Même mené et ligue 1 en direct streaming infériorité numérique necklace toute la deuxième période, l’OM a montré
    du caractère et des intents louables, même
    si Darder (56e) puis Tolisso pouvaient creuser l’écart pour Lyon (58e).

  10. Florida comments:

    These are actually a picked, grown and processed
    form of green coffee bean capsules (http://www.amazon.com) with Chlorogenic Acid as an active
    component in it.

  11. Daisy comments:

    These technicians concentrate on giving TELEVISION fixing
    and also setup in addition how to fix a computer (Nickolas) outlet relocation solutions.

  12. Phillip comments:

    Young recommends examining where to get pest control
    make sure the extermination firm has the required state and neighborhood certifications, and verify
    that they have a qualified entomologist functioning within the company that can deal with
    as well as review your worries.

  13. Lenard comments:

    Contact us today to ask about our package on inflatable video games
    and other event leasings.

    my webpage Location de structure Gonflable à Gatineau (Hassie)

  14. Shane comments:

    Next to the checklist, as you can see, you could add, edit, delete, import, or reset
    blog site search engine optimisation – Garland – engines.

  15. Sandy comments:

    They likewise supply wood flooring refinishing, as well as residence hosting, besides restroom makeover.

    my weblog: bathroom remodeling new berlin wi

  16. Cecilia comments:

    The limited variety of people seeking operate in pest control info control as well as the have to replace workers
    that leave this occupation must cause many job openings.

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

  18. 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*/ }

  19. 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() ) : ?>

  20. 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=""> <s> <strike> <strong>