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

13 Responses to Styling the First Post Different

  1. Elena comments:

    It’s an awesome article for all the web viewers; they will obtain advantage
    from it I am sure.

    Also visit my webpage … spider-man dress up,

  2. lisaen3 comments:

    Scandal porn galleries, daily updated lists

    free mms porn videos download unlimited sex intruction video porn stream free vampire porn free gals and doggie porn streaming mature vs young porn

  3. Austin comments:

    Dark beatss

    beatmakers spookyvibes flgang flstudio musicproductions darkvibes typebeat rap
    trap soundcloudrapper rappers instrumentals
    beatsforlease rokit midi controller stand (Blanca) keyboard dj
    studiolife producerlife songwriter hiphopmusic

  4. Brain comments:

    Ihr könnt das Nektar Panorama P4 MIDI-virtual keyboard mac (https://www.coimbatoreliving.com) an Euren PC anschließen und die Musik machen, die Euch gefällt

  5. Amelie comments:

    KDEActivePlasmaOne: Virtual midi keyboard audacity (http://www.alphaschool.in/blog/groups/why-all-the-stock-exchanges-collapsed/) works properly.
    Openstreetmap greets from the desktop, hurra. Lets seek for German lang-packs…

  6. Brandon comments:

    Technical analysis indicators are choppier and fewer fluid than on excessive quantity securities.Less Data More DecisionsIf
    youve ever taken a look at a stock chart of a low volume stock,
    youll see that technical analysis doesnt apply as well. You’ll additionally see some extra software
    program included in packages which typically will
    increase the value tag, so keep that in mind as effectively.

    You’ll receive picks from red-sizzling microcap stock in addition to midi keyboard an pc anschließen (http://game-cotuong.com/forum/profile.php?id=171924) extra way to money in on penny stocks.
    Stock photography is a good passive revenue supply for
    photographers and in the event you like it, it might be your principal
    supply. Do you want the most well liked media stories and opinions
    on bluetooth earphones? Bluetooth software program and Bluetooth appropriate accessories is important
    to use Bluetooth wireless know-how. Another profit to Bluetooth is that it operates at a
    frequency of 2.Four GHz, which allows it to be license free and broadly available all through
    the world. Today’s fast paced world wants the
    optimum worth for cash and that too quick. Today’s
    internet market has seen it’s share of digital marketers cropping up each different day.

    It’s incredible precisely what you may develop if you actually verify the ‘pre-packages’ church aisles of
    the supermarket.

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

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

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

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


This site uses Akismet to reduce spam. Learn how your comment data is processed.