Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm displaying two posts on my main page, and using post pagination to go through the older posts.

My problem is I want to change the layout a bit of the most recent post. This sounds like a very basic question, and I have seen a lot of answers, but they only solve the problem for the first post on the page, not the actual -one and only- most recent post.

Right now I tried something like this:

<?php if(have_posts()):?>
   <?php $pcount = 0;?>
<?php endif;?>

<?php while (have_posts()): the_post(); ?>
   <?php $pcount++;?>
   <?php if($pcount < 2): ?>
       /*some html changes*/
   <?php endif; ?>
<? endwhile; ?>

This worked great. The first post on the page, which is by default the most recent one, has a changed layout. But when I navigate to the next page, I see the same. The first post (which is actually the third recent post, not the most recent), is also changed...

So, my actual question: How can I check if the post is actually the single most recent one. Not the first on the page.

Thanks

share|improve this question
add comment

2 Answers 2

up vote 0 down vote accepted

$pcount is counting how many posts you've gone through in the current loop, it isn't counting the number of pages.

Think for a moment about what the loop is doing, if you're on page 2, why would you have the posts from page 1? It makes no sense, which is a sign that thinks aren't going to work.

If what you have was ever going to work then this:

while (have_posts()) {
    the_post();
    the_title();
}

Would print out the title of all the posts on page 2, and page 1. This is not what happens. The main loop code would need to have some kind of hint as to what your intentions were which it doesn't.

So your question is actually:

How do I determine the current page?

For which we can see an answer here:

http://stackoverflow.com/questions/1031442/finding-current-page-number-in-wordpress

Namely:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
share|improve this answer
1  
This works! Thank you for the explanation. –  Decor Mar 27 '13 at 13:19
add comment

the conditional tag is_paged() can be used to limit the changes to the first of pages;

for example, change your conditional statement from:

   <?php if($pcount < 2): ?>

to:

   <?php if($pcount < 2 && !is_paged() ): ?>
share|improve this answer
    
This works! Thank you. I accepted the other answer, because it goes a little more in depth. I would like to up vote your answer, but haven't got enough rep. –  Decor Mar 27 '13 at 13:21
    
no problem - you are welcome ;-) –  Michael Mar 27 '13 at 13:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.