WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Identifying most recent post for alternate styling (5 posts)

  1. pshero
    Member
    Posted 5 years ago #

    Hey gang, I need a little PHP guru help!

    I'd like to style the most recent post on my homepage differently than the rest. I've written a counter script (shown below) but it resets on the rest of the pages so the first post on each subsequent page is also styled as if it were post #1. Here's my code:

    '
    <?php $post_number = 0; ?>
    <?php while ( have_posts() ) : the_post() ?>
    <?php $post_number++; ?>

    <div <?php if ( $post_number == 1 ) { ?>class="article_preview_featured"<?php } else { ?>class="article_preview"<?php } ?>>

    >> MY POST CONTENT WOULD BE HERE <<

    </div>
    <?php comments_template() ?>
    <?php endwhile ?>
    '

    Any help would be GREATLY appreciated!

  2. pshero
    Member
    Posted 5 years ago #

    After an ass-load of trial and error I got this little trick to work perfectly. The following script allows you to style your most recent post differently than the rest of the posts on your page. It will only alter the most recent post and won't effect the top posts on preceding pages. For illustration's sake I'm calling style for the first post "featured" and the style for all other posts "regular".

    Once you've setup your index.php page with the following code, you'll need to edit your style.css page and add styles for .featured and .regular so that they look differently.

    I'm including the entire WP loop. Just stick your post script where the comments tell you to!


    <?php while (have_posts()) : the_post(); ?>
    <!-- EVEN IF YOU'VE GOT PAGEST OF POSTS AFTER THE HOMEPAGE, ONLY THE FIRST POST ON THE FIRST PAGE WILL BE STYLED DIFFERENTLY -->
    <?php if (is_paged()) : ?>
    <?php $postclass = ('regular'); ?>
    <?php else : ?>
    <?php $postclass = ($post == $posts[0]) ? 'featured' : 'regular'; ?>
    <?php endif; ?>

    <!-- NOW THAT WE'VE SET UP THE CONDITIONS, LETS CREATE A DIV WITH THE CSS CLASS FED DYNAMICALLY -->
    <div class="<?php echo $postclass; ?>">

    <!-- THIS IS WHERE ALL THE SCRIPT FOR YOUR POST GOES -->

    <?php comments_template() ?>
    <?php endwhile ?>

    I hope this helps somebody, cuz it took me hours to figure out!

  3. smallbizpod
    Member
    Posted 5 years ago #

    Thanks for this. A really helpful and simple piece of code. It's led me to find the perfect solution for people wanting a 'full content' first post followed by excerpt posts on the front page and all subsequent pages.

    Here's the code:

    <?php ($post == $posts[0] && !is_paged()) ? the_content('', 'FALSE') : the_excerpt() ; ?>

    Works like a dream and avoids offsets, rewinds and multiple loops.

  4. Grazoink
    Member
    Posted 5 years ago #

    can oyu give me an example of the css u would use for this
    I dont get it!!

  5. patrickrhill
    Member
    Posted 5 years ago #

    Thanks a lot. This was very helpful. One thing I altered though was changing the class "regular" to simply "post", because that is what the default class is set to in wordpress. That way your original post styles that were already set will all still be there after this code is implemented. Only the first post will change.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.