Forums

[resolved] if(is_latest()) exist? (7 posts)

  1. Syni
    Member
    Posted 6 years ago #

    Is there any tag that allows you to tell if a post is the latest post? I've searched and couldn't find one.

  2. moshu
    Member
    Posted 6 years ago #

    I don't remember anything like that. And I don't even think is needed - if you have your Loop to show 1 (one) post, that's always the latest (unless you tell it to do otherwise :).

  3. Syni
    Member
    Posted 6 years ago #

    Yeah, but I want certain images to change depending on if the post is the latest or not. So, I'm confused. :p

  4. Joshua Sigar
    Member
    Posted 6 years ago #

    You could probably do something like the following inside the Loop.

    if ($wp_query->current_post == 0 && is_home()) // latest post
    {

    }

    EDIT: make sure to use that code only in the front page
    EDIT2: ok. I edited the code above. should be more bulletproof now.

  5. Syni
    Member
    Posted 6 years ago #

    Thanks Alphaoide. Is there a way to do this on a single post page too?

  6. Joshua Sigar
    Member
    Posted 6 years ago #

    The following solution makes my previous solution obsolete.

    What you want to do is
    (Before the Loop begins)
    1. Get one latest post with template tag get_posts
    2. Get the date of that latest post.
    (Inside the Loop)
    3. Compare the current post's date with latest post's date

    So, step 1 and 2 would be...
    <?php
    $latest_post = get_posts('numberposts=1');
    $latest_date = $latest_post[0]->post_date;
    ?>

    and step 3...
    <?php
    if ($latest_date == $post->post_date) :
    echo "LATEST";
    endif;
    ?>

  7. Syni
    Member
    Posted 6 years ago #

    Thank you! That works perfectly!

Topic Closed

This topic has been closed to new replies.

About this Topic