WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] New status/flag/ribbon for posts (28 posts)

  1. jdestree
    Member
    Posted 4 years ago #

    Hi again,

    I am looking for a way or plugin to display a flag on a post as new for a certain period of time. I have been searching the plugins and have not came across anything yet. Any ideas?

  2. MichaelH
    Member
    Posted 4 years ago #

    Not too sure what 'flag' you want to use, but something like this in a post loop should work:

    <?php
    //display message if post is less than 46 days old
    $mylimit=46 * 86400; //days * seconds per day
    //$post_age = date('U') - get_post_time('U');
    $post_age = date('U') - mysql2date('U', $post->post_date_gmt);
    if ($post_age < $mylimit) {
    echo '&hearts; This post is still new &hearts';
    }
    ?>
  3. jdestree
    Member
    Posted 4 years ago #

    Would there be a way to throw another variable into the mix and say...

    If there is a tag of "new" on a post, display the new icon for a certain period of time. If there is a tag of "updated" on a post, display that updated icon for a certain period of time and lastly, if there is a tag of "final" display that corresponding icon for a certain period of days?

    The reason I ask is that this blog will have some posts that short term.

    Thanks for your help!!!

  4. jdestree
    Member
    Posted 4 years ago #

    Thoughts on the adding a 'flag' with tags??

  5. MichaelH
    Member
    Posted 4 years ago #

    A bit 'wordy' but:

    <?php
    // get post tags, if post less than certain days and has specific tag, display message
    $tags = wp_get_post_tags($post->ID);
    $tagsarray = array();
    foreach ($tags as $tag) {
      $tagarray[] = $tag->name;
    }
    $taglist = implode(',', $tagarray);
    $newlimit = 90 * 86400; //days * seconds per day
    $newtag = 'new';
    $updatedlimit = 60 * 86400; //days * seconds per day
    $updatedtag = 'updated';
    $finallimit = 30 * 86400; //days * seconds per day
    $finaltag = 'final';
    $message = '';
    $post_age = date('U') - mysql2date('U', $post->post_date_gmt);
    if ( ($post_age < $newlimit) && (strpos($taglist, $newtag) !== false) ) {
      $message = '&hearts; This post is new &hearts;';
    }
    if ( ($post_age < $updatedlimit) && (strpos($taglist, $updatedtag) !== false) ) {
      $message = '&clubs; This post was updated &clubs;';
    }
    if ( ($post_age < $finallimit) && (strpos($taglist, $finaltag) !== false) ) {
      $message = '&diams; This post is final &diams;';
    }
    if ($message) {
      echo $message;
    }
    ?>
  6. jdestree
    Member
    Posted 4 years ago #

    Thank you!

  7. s_ha_dum
    Member
    Posted 4 years ago #

    If there is a tag of "new" on a post, display the new icon for a certain period of time. If there is a tag of "updated" on a post, display that updated icon for a certain period of time and lastly, if there is a tag of "final" display that corresponding icon for a certain period of days?

    Yes, MichaelH told you how to do this. Just echo an img tag instead of text and add some ifelse's to catch your other conditions.

    If you use tags you are going to have to manually updating them to make this work. That's a lot of work. WordPress tracks the post date and the last updated. Follow MichaelH's advice and use those to do the work for you.

  8. jdestree
    Member
    Posted 4 years ago #

    wait, this only removes the image after the time right? Not the entire post?

  9. jdestree
    Member
    Posted 4 years ago #

    @apljdi I wish I could do it with post date but because of the use of this blog install, I need the option to do it manually

  10. jdestree
    Member
    Posted 4 years ago #

    hmmmm, I can't seem to get this to work. All I have to do is add the code in the loop and add tags on my posts, correct?

  11. jdestree
    Member
    Posted 4 years ago #

    I'm getting there.

    I managed to get it to work with two issues:

    1.) When the post has no tags, the previous message appears. i.e. First post was new, so the second post also shows new because it has no tags.

    2.) The messages appear two in each post.

  12. MichaelH
    Member
    Posted 4 years ago #

    Yes well post your whole template at wordpress.pastebin.ca and report the link back here.

  13. jdestree
    Member
    Posted 4 years ago #

  14. MichaelH
    Member
    Posted 4 years ago #

    Go back and look at my code above -- you changed around and messed up the message variable.

  15. jdestree
    Member
    Posted 4 years ago #

    I went back to the code above and still get incorrect flags.

    Here is an image of my result: http://www.postimage.org/image.php?v=aV10djxr

    You will see I added an echo $tagsarray inside the if ($message) {
    echo $message;
    }

    Each post only has one tag associated with it: either new, final, updated or no tag at all

    You will see the tags seem to stack.

    Any ideas?

  16. jdestree
    Member
    Posted 4 years ago #

    I am going crazy. I cannot figure this out! I've been working on this problem for many hours now.

  17. MichaelH
    Member
    Posted 4 years ago #

    Okay I will give this one last try. Paste your code in a pastebin and I'll take a look at it.

  18. jdestree
    Member
    Posted 4 years ago #

    http://wordpress.pastebin.ca/1684061

    Thanks for all your help. I am driving myself crazy trying to figure this out...

  19. MichaelH
    Member
    Posted 4 years ago #

    It looks okay, so now I need to see a URL for those posts with 'the_tags' displayed. Hoping to see the tags, "New", "Updated", or "Final"

  20. jdestree
    Member
    Posted 4 years ago #

    How do I get you that? This is an internal blog so I can direct you to a specific address...

  21. MichaelH
    Member
    Posted 4 years ago #

    Well then I can't help you anymore. Sorry. That code I suggested above worked for me so maybe someone else can spot the problem.

  22. jdestree
    Member
    Posted 4 years ago #

    All I know is that when I add a tag that is not one of the three, thats when the problems occur

  23. MichaelH
    Member
    Posted 4 years ago #

    No problem using multiple tags on a post. Just tested it.

    Again check your tags and check your post dates. Both have to be correct for this to work.

  24. jdestree
    Member
    Posted 4 years ago #

    Can you post your entire page code? It might be helpful if I can see how you set things up

  25. MichaelH
    Member
    Posted 4 years ago #

    While getting my index.php ready to post (had a bunch of junk in it) I noticed a problem.

    This line

    $tagsarray = array();

    should be this:

    $tagarray = array();

    Here's the index.php as used for the WordPress Default Theme
    http://wordpress.pastebin.ca/1684291

  26. jdestree
    Member
    Posted 4 years ago #

    That was the problem!!!! Thank you so much!!!!

  27. jdestree
    Member
    Posted 4 years ago #

    One last question, how long is this:

    60 * 86400; //days * seconds per day

    How many days? I don't understand the formula.

  28. MichaelH
    Member
    Posted 4 years ago #

    That's 60 days.

    86400 is the number of seconds in a day.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.