Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a videosite based on wordpress. I would like it to show a icon on the posts from today. I've tried googling it, but without any succes at all. Any ideas on how to do it or maybe someone has a link to a tutorial on it?

Thanks in advance :-)

share|improve this question

1 Answer 1

up vote 0 down vote accepted

you can use the difference between the post time (get_the_time()) and the current time (date()) -

for instance, this snippet added to functions.php of your theme would add a new css class to post_class() if the post is less than 24hrs old:

function post_from_today_class($class) {
//add .new-post-today to post_class() if newer than 24hrs
    global $post;
    if( date('U') - get_the_time('U', $post->ID) < 24*60*60 ) $class[] = 'new-post-today';
    return $class;
}

add_filter('post_class','post_from_today_class');

you can use the .new-post-today class to style something differently in your post div.

the core idea is:

if( date('U') - get_the_time('U', $post->ID) < 24*60*60 ) { /*do something*/ };

edit: alternatively, direct in a template with its own php tags, use:

<?php if( date('U') - get_the_time('U', $post->ID) < 24*60*60 ) : ?>text<?php endif; ?>
share|improve this answer
    
Thanks for your reply! I've put the code in functions, but then adding this to the loop: <?php if( date('U') - get_the_time('U', $post->ID) < 24*60*60 ) { /*do something*/ }; ?> test <?php endif; ?> i get the following error Parse error: syntax error, unexpected T_ENDIF in public_html/wp-content/themes/WPTube4/index.php on line 82 –  Torben Mar 1 '12 at 20:08
    
Remove the curly braces ("{}") and insert a colon before /*do something*/. –  Joseph Mar 1 '12 at 20:22
    
if you want to use the one-liner code in the loop, don't add the larger code to functions.php. the error is from adding extra text and an 'endif' after the code; I'll edit my answer. just see, that @Joseph answered it already. –  Michael Mar 1 '12 at 20:23
    
As you may have figured out by now, i'm pretty much a noob. Thanks for your patience :-) The code now looks like this: <?php if( date('U') - get_the_time('U', $post->ID) < 24*60*60 ); ?> test <?php endif; ?> And gives me the following error: Parse error: syntax error, unexpected T_ENDIF in /var/www/titanen.dk/public_html/spillersmart/wp-content/themes/WPTube4/index.php on line 82 –  Torben Mar 1 '12 at 20:28
    
What should "close" the function, if not <?php endif; ?>? –  Torben Mar 1 '12 at 20:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.