WordPress.org

Ready to get started?Download WordPress

Forums

Post Dates that indicate Today, Yesterday, or the post date (3 posts)

  1. hsatterwhite
    Member
    Posted 4 years ago #

    Hello,
    I am trying to create a recent post list that includes the post date, but I would like the post date to read "Today", "Yesterday", or the actual post date if the post was not published today or yesterday. I've got the recent list code working just fine, but I can't seem to figure out how to do the Today and Yesterday part.

    This is the recent post code I am using:

    <?php
    	  $args=array('showposts'=>10,'caller_get_posts'=>1);
    	 $my_query = new WP_Query($args);
    
    	  if( $my_query->have_posts() ) {
    	    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    	      <li><?php the_time('l F d, Y'); ?> - <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    	    <?php endwhile; } ?>

    Thanks :)

  2. whooami
    Member
    Posted 4 years ago #

  3. tomconte
    Member
    Posted 4 years ago #

    Fancy I see you here. It seems we both are pondering the same thing. Well, I found a solution although I'm having problems with actual date it is interpreting (today is the 21st, but all of sudden it's declaring itself as tomorrow 22nd).

    Anyways, here is what I have to display posts published for "Today":

    // Capture today's date
    $today = getdate();
    
    // Set WP Query arguments
    $argsT = array(
    	'cat'=>3,
    	'year'=>$today["year"],
    	'monthnum'=>$today["mon"],
    	'day'=>$today["mday"]
    	);
    
    // Create a new instance of the object
    $todayQuery = new WP_Query($argsT);
    
    // Loop through posts published "Today"
    <?php if ($todayQuery->have_posts()) : ?>
    <?php while ($todayQuery->have_posts()) : $todayQuery->the_post(); ?>
    <a href="<?php echo $link; ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    <?php endwhile; ?>

    My only problem is the getdate() function is much faster than what the actual time is....not sure why but I'm still figuring that out.

Topic Closed

This topic has been closed to new replies.

About this Topic