WordPress.org

Ready to get started?Download WordPress

Forums

how to check if a post is the newest post (2 posts)

  1. bikerjeg
    Member
    Posted 6 years ago #

    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.

  2. wp_guy
    Member
    Posted 6 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic