WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Style latest post (7 posts)

  1. pad
    Member
    Posted 6 years ago #

    Hi all.

    I want to style my latest post on the homepage ONLY.

    All entries are in an entry-head div.

    This is my code right now.

    <?php if ($postclass = ($post == $posts[0]))
    {
    echo '<p class="topstory">TOP STORY</p>';
    }
    ?><?php } ?>
    
    <?php if (is_paged()) : ?>
    				<?php $postclass = ('entry-head'); ?>
    			<?php else : ?>
    				<?php if (is_home('')) { ?><?php $postclass = ($post == $posts[0]) ? 'entry-head firstpost' : 'entry-head'; ?><?php } ?>
    			<?php endif; ?>
    
    <div class="<?php echo $postclass; ?>">

    It all works except on archive pages the p class="topstory">TOP STORY</p> displays, like page 2, page 3 etc. How do I stop this?

    Is this code I used, did I do it right? If not, could you suggest an alternative?

    Thanks a lot.

  2. Khairil Zhafri
    Member
    Posted 6 years ago #

    What exactly you are trying to do?

    Is it something like this? Then, I've offered a solution.

  3. pad
    Member
    Posted 6 years ago #

    I don't think what you suggested is quite what I want. Basically, I want this. Take this as the homepage:

    LATEST POST:
    Post Title 1
    Blah blah blah blah (read more)

    Post Title 2
    Blah blah blah blah (read more)

    Post Title 3
    Blah blah blah blah (read more)

    Post Title 4
    Blah blah blah blah (read more)

    Post Title 5
    Blah blah blah blah (read more)

    The most recent post should obviously have the "Latest Post" bit echoed but also be in a div with class "entry-head firstpost". All other entries are in a div with class "entry-head".

    It's all conditional tags and stuff isn't it?

  4. Khairil Zhafri
    Member
    Posted 6 years ago #

    Honestly, I am not so familiar with the ($postclass = ($post == $posts[0])) bit. I have tried to use it but it made the following lines so complicated.

    You can still use what I suggest.

    <?php if (have_posts()) : ?>
     <?php query_posts("showposts=1"); // show one latest post only ?>
      <?php while (have_posts()) : the_post(); ?>
       <div class="entry-head firstpost">
       LATEST POST:
       <!-- all the usual template tags here i.e. title, excerpts, date, etc.  -->
       </div>
      <?php endwhile; ?>
     <?php query_posts("showposts=4&offset=1"); // show 4 latests posts excluding the latest ?>
      <?php while (have_posts()) : the_post(); ?>
       <div class="entry-head">
       <!-- all the usual template tags here i.e. title, excerpts, date, etc.  -->
       </div>
     <?php endwhile; ?>
    <?php else: ?>
     <!-- Error message when no post published -->
    <?php endif; ?>

    It may be a bit longer since you repeat most of the template tags twice but it works.

  5. Kafkaesqui

    Posted 6 years ago #

    Change the first line to:

    <?php if ( !is_paged() && ($post == $posts[0]) )

    In some cases poppacket's suggestion will be useful, but for simply styling the first post differently, it's overkill.

  6. pad
    Member
    Posted 6 years ago #

    Thanks to both of you.

    Kafkaesqui, that line did the trick though!

  7. pshero
    Member
    Posted 5 years ago #

    I think this is exactly what you're after http://wordpress.org/support/topic/212494

Topic Closed

This topic has been closed to new replies.

About this Topic