Support » Themes and Templates » how to check if a post is the newest post

  • Is there a way to check if a post is the latest or newest post? I am trying to hardcode adsense into a template but I only want to display adsense below the content of the newest post. I looked through the codex and did a few searches but I can’t seem to find a function that would help with this.

    If anyone can help me by pointing me to the right direction I would really appreciate it.

    Thanks in advance.

Viewing 1 replies (of 1 total)
  • You can add the following code to your index.php (outside the loop):

    <?php $post_number = 0; ?>

    Then this code inside the loop:

    <?php $post_number++; ?>

    The first piece of code declares the variable $post_number and sets it’s value to 0. The second runs every time a post is echoed and ads 1 to the $post_number variable.

    Now you can use the following if statement:

    <?php if ( $post_number == 1 ) : ?>
        Your adsense code here
    <?php endif; ?>

    Hope that helps!

Viewing 1 replies (of 1 total)
  • The topic ‘how to check if a post is the newest post’ is closed to new replies.