3

I need the first post that was ever posted to be styled differently. Is there a way that I can see if the post is first, and then change its contents? I currently have a div in all my posts. This needs to be replaced with different div.

Apparently the following piece of code can help, but I'm not sure how to implemenet it:

<?php if (have_posts()) : $postCount = 1; while (have_posts()) : $postCount++; ?>

I'm new with WordPress so not entirely sure how this could work?

  • This question appears to be off-topic because it is about basic understanding of programming concepts – SergeS Mar 02 '14 at 09:51

2 Answers2

9

Append following codes after yours:

<?php if (have_posts()) : $postCount = 1; while (have_posts()) : $postCount++; ?>

<?php if($postCount == 2) { ?>
  // SOMETHING TO DO WITH FIRST POST
<?php } else { ?>
  // SOMETHING TO DO WITH ALL OTHER POSTS
<?php } ?>
Bora
  • 10,529
  • 5
  • 43
  • 73
  • Would I put any after code the first `if` statement, if I wanted to change the divs completely? –  Aug 21 '13 at 12:35
  • @tmyie updated with `FIRST POST` and `OTHER POSTS` area – Bora Aug 21 '13 at 12:40
  • 1
    Okay, here's my code as a result of your answer: http://pastebin.com/pjDnXJU9 I've changed the divs `up` and `down`. Unfortunately, I get an error? –  Aug 21 '13 at 12:43
  • @tmyie Great! What problem now? – Bora Aug 21 '13 at 12:45
  • Here's my entire index.php: http://pastebin.com/TZ5DJghe (It's small). For some reason there's an error on get_footer() at the bottom. There wasn't an error before the `if posts` alteration? –  Aug 21 '13 at 12:47
  • You forgot end if, replace `` with `` – Bora Aug 21 '13 at 12:49
  • Ah yes, thanks. For some reason the entire code is still not working: http://herbert-theme.info/. I was hoping to see the first post styled differently, and then all the others the same. –  Aug 21 '13 at 13:02
  • Where area is not working? – Bora Aug 21 '13 at 13:07
  • Thanks so much for your help. If you revisit the page, it is displaying the *latest* post differently, it needs to the the *oldest* . –  Aug 21 '13 at 13:09
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35920/discussion-between-tmyie-and-bora) –  Aug 21 '13 at 13:12
  • I guess, this is about `css` – Bora Aug 21 '13 at 13:14
3
<?php if (have_posts()) : $postCount = 0; while (have_posts()) : $postCount++; ?>

The above piece of code will create a $postCount variable and increment it every time The Loop loops. Note that I've changed it to start at 0 instead of 1.

We now have the post count in $postCount variable. We just need to find the first post and apply the styles to that post.

Normally, you'll have something like this:

<div class="post" id="post-<?php the_ID(); ?>">

Change that to:

<div <?php if($postCount == 1) { ?>class="YourSpecialClass"<?php } 
else { ?>class="post"<?php } ?> id="post-<?php the_ID(); ?>">

The above code will check if the $postCount is 1 (first post), and then add the class="YourSpecialClass" part as its <div> attribute.

A better readable version:

<?php if($postCount == 1) { ?>

    //the first post -- style it

<?php } else { ?>

    //other posts -- proceed normally

<?php } ?>

Hope this helps!

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • At the moment I do not have a post `the_ID`, I've just got `the_content()` and `the_title()`. Would that alter the solution? –  Aug 21 '13 at 12:34
  • @tmyie: no, it won't. That was just an example. – Amal Murali Aug 21 '13 at 12:35
  • You've got `$postCount == 2`, to affect all posts after one, would that be `$postCount => 1` ? :) –  Aug 21 '13 at 12:37
  • @tmyie see the updated answer. I've changed the `$postCount` variable to start at 0 and then have added a better readable version of the if-else block so you get a better idea. – Amal Murali Aug 21 '13 at 12:47
  • Thanks! And I would place this within the standard `` loop? –  Aug 21 '13 at 12:59
  • @tmyie: that's exactly where this piece of code needs to be added ;) – Amal Murali Aug 21 '13 at 13:03