• Resolved grintoul

    (@grintoul)


    Hi everyone,

    Was wondering if I could get a bit of help. Have looked at the forums but all related solutions I find mention variables such as $post, which I don’t have in my loop below I don’t think? What I want to do is check if each post in the loop was posted within the last 12 hours, and if so display an image (it’s just a small icon to alert the user that it’s a recent post). The simplified code for my loop is:

    <?php while (have_posts()) : the_post(); ?>

    <div class="post">

    <h2 id="post-<?php the_ID(); ?>"><a>"<?php the_title();</a></h2>

    <small><?php the_time('F jS') ?></small>

    <div class="entry">
    <?php the_content('Read more �'); ?>
    </div>

    </div>

    <?php endwhile; ?>

    Would really appreciate any help anyone can give me – basically, if someone can tell me what the php IF statement is to check if it was posted in the last 12 hours, I know how to do the if… else and insert the image.

    Thanks in advance,

    Guy

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter grintoul

    (@grintoul)

    Sorry – I’ve posted this in the wrong category. Have re-posted under templates as don’t know how to change the category. If an admin could delete this version of the post I’d appreciate it. Sorry!

    Adam Brown

    (@adamrbrown)

    Start here: http://codex.wordpress.org/Template_Tags#Date_and_Time_tags

    You want get_the_time(), not the_time(), for your “if” statement. (You can leave the_time() as is for displaying the time, though.)

    Try this:

    if ( (current_time(timestamp) - get_the_time('U')) < 43200 ){

    That checks whether the current time is less than 43,200 seconds (12 hours) more than the post’s time stamp.

    If that doesn’t work right, you may need to set the GMT offset in current_time(). Details: http://codex.wordpress.org/Function_Reference/current_time

    Thread Starter grintoul

    (@grintoul)

    Hi

    Thanks for your reply – worked a treat 🙂 And also reminded me I’ve not yet changed my system time to reflect daylight savings!

    However, even once I did it’s still an hour out… I’m 1 hour ahead of GMT so I think I do need to set the GMT offset. Would you be able to tell me how to do this? Had a look at http://codex.wordpress.org/Function_Reference/current_time but it’s incomplete.

    Thanks again

    Guy

    Thread Starter grintoul

    (@grintoul)

    Hi… ignore that. Just figured out why it wasn’t working… offset is in hours, so needs to be multiplied by 3600 to get seconds. Full line I needed was:

    if ( (current_time(timestamp) – get_the_time(‘U’) – (get_settings(‘gmt_offset’) * 3600) ) < 43200 ){

    Thanks again for all your help, I really appreciate it!

    Care to share the complete loop you used? Looking for this functionality too but have no clue where to place your solution..

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Was a post posted within last 12 hours?’ is closed to new replies.