Forums

if date of post is earlier than specific date, then only display Year not more (20 posts)

  1. Charbax
    Member
    Posted 3 years ago #

    I am posting some content which is old and which I don't know the exact date of release.

    Thus for every post dated older than 2003 I would like my Wordpress to only display the Year, and not "August 15, 1994 at 11:54 am".

    This is the code that I have in the index.php of the theme that I am using:

    <p class="postmeta">
    <?php the_time('F j, Y') ?> <?php _e('at'); ?> <?php the_time() ?>

    How do I do a "if the_time is older than specificdate then only display Y".

    Thanks for your help!

  2. ryanfitzer
    Member
    Posted 3 years ago #

    The only solution I could think of would be to have a category for such posts and then use a specific category-#.php (# = the id of the specific cat) file to show them where you adjust <?php the_time('F j, Y') ?> <?php _e('at'); ?> by taking out the "F j," part to leave only the year.

    This describes how to make a category template.

    http://codex.wordpress.org/Category_Templates

    This is only half the battle though. Then you'll need this great plugin so you can define a "single-cat-#.php file" for when your view a single post in the category. Once again, you'll need to make the same edits as above.

    http://guff.szub.net/2005/07/21/post-templates-by-category/

    This may be a long way around, but it will get you there.

  3. tsguitar
    Member
    Posted 3 years ago #

    Try this:
    <p class="postmeta">
    <?php if(get_the_time('Y') < 2003)
    {
    the_time('Y');
    }
    else
    {
    the_time('F j, Y at g:i a');
    }
    ?>

    You may need to put more backslashes around the "\a\t"; the PHP manual isn't too clear on that. Some experimentation should get it working quickly.

    Here are the resources I used, in case they help you:
    http://us3.php.net/manual/en/function.date.php
    http://codex.wordpress.org/Formatting_Date_and_Time
    http://codex.wordpress.org/Template_Tags/get_the_time

  4. Otto42
    Moderator
    Posted 3 years ago #

    The function you want to know about is get_post_time(). It takes parameters, but if you call it by itself, with no parameters, it gives you back the time the post was made in seconds since the epoch, which is a rather useful format.

    Here's some code that will work for you:
    <p class="postmeta">
    <?php
    $now = time();
    $compare_time = mktime(0, 0, 0, 1, 1, 2003);
    $post_time = get_post_time('U');
    if ($post_time < $compare_time) {
    the_time('Y');
    }
    else {
    the_time('F j, Y'); _e('at'); the_time();
    }
    ?>

    Edit: tsguitar's method is a bit more specific to your problem than mine is, but if you want a more generic solution, this will still help you.

  5. tsguitar
    Member
    Posted 3 years ago #

    Otto, would our two examples do the same thing?

  6. Otto42
    Moderator
    Posted 3 years ago #

    tsguitar: Yes, basically. The mktime function takes arguments of hour, minute, second, month, day, year and produces a time since the epoch value you can compare against. Makes it a bit easier for more complex date math.

  7. Charbax
    Member
    Posted 3 years ago #

    Thanks a lot! Both tsiguitar's and Otto42's codes work great, I just added the backslash to tsiguitar's as he said I might have to:

    the_time('F j, Y \a\t g:i a');

  8. KatGirl
    Member
    Posted 3 years ago #

    Otto42 & tsguitar - you guys are worthy of a Gold Star!

    I had some old articles sitting in my files going to waste because I couldn't work out how to display the copyright material in year only (2004) using the standard time call that is also used for recent material.

    Now I can display old articles with new material side by side with complete ease.

    Just brilliant!

  9. tsguitar
    Member
    Posted 3 years ago #

    Excellent. I'm really glad this worked out so well. Can you mark this "Resolved," then?

  10. KatGirl
    Member
    Posted 3 years ago #

    Can I do that?
    Am I suppose to do that?

    Or is it the OP's call?

  11. tsguitar
    Member
    Posted 3 years ago #

    It's the OP's call. This all worked for you and it all worked for Charbax, so I hope this gets marked "Resolved" so everyone else knows it's taken care of.

  12. vkaryl
    Member
    Posted 3 years ago #

    OP's generally, but if neither of you do it by tomorrow, I or one of the other mods will. Looks like you both got what was needed....

  13. KatGirl
    Member
    Posted 3 years ago #

    I'm a little lost...

    I'm looking at my screen and I can't see any "click" section that will allow me to mark this thread as resolved.

    Am I looking in the right place? I'd like to mark this as resolved, if Charbax doesn't - responsible adult that I am...

  14. vkaryl
    Member
    Posted 3 years ago #

    Top of screen, just above the bolded "add this topic to your favorites" link....

  15. KatGirl
    Member
    Posted 3 years ago #

    Thanks vkaryl.

    This is what I have...

    Topic started 1 day ago - text
    14 posts so far - text
    Latest reply (link) from vkaryl - text
    This topic is not resolved - text, no link
    Add this topic to your favorites (?) (link)

    Is this what you mean?

  16. vkaryl
    Member
    Posted 3 years ago #

    Hoo. Your "This topic is not resolved - text, no link" isn't a dropdown box with a "Change" button to the right?

    That's weird. Do you have some stringent setting for FF for instance, using NoScript or something?

  17. KatGirl
    Member
    Posted 3 years ago #

    The "This Topic..." is simple text, no drop down box with a change button attached.

    I don't know if I do or don't have special settings. I'm using multiple browsers at once (FF & IE6) and I have TREND MICRO PC-cillin Internet Security 2006.

    I haven't encountered anything like this before, but I have to ask: Will that affect things?

  18. Otto42
    Moderator
    Posted 3 years ago #

    KatGirl: You're not the original poster of this thread, so you wouldn't be able to mark it as resolved.

  19. vkaryl
    Member
    Posted 3 years ago #

    Oh. Oops. Forgot about that by this time. Thanks Otto....

  20. KatGirl
    Member
    Posted 3 years ago #

    Whew! I'm glad to hear it I was wondering if my computer was on the blink...

Topic Closed

This topic has been closed to new replies.

About this Topic