WordPress.org

Ready to get started?Download WordPress

Forums

Show Last Updated Date/Time (13 posts)

  1. mystifier
    Member
    Posted 5 years ago #

    I would like to show the most recent update Date/Time in my template.

    Being completely green when it comes to php, can some kind soul show me the simplest way of displaying the result of:
    'select max(post_date) from wp_posts'

    Thanks.

  2. nlex
    Member
    Posted 5 years ago #

    Same here : i would like to display the date of the last update of the whole site (in my footer). Any idea ?

  3. nlex
    Member
    Posted 5 years ago #

    same

  4. doppelboxx
    Member
    Posted 5 years ago #

    this might help you:

    /* site_last_updated
    *
    * This will check the modified_date of all posts and echo the most recent date.
    *
    */
    function site_last_updated($d = '') {
      $last_update = 0;
      if ( have_posts() ) :
        while ( have_posts() ) :
          the_post();
          // check for current post's modified_date
          if (get_the_modified_date('U') > $last_update) {
            $last_update = get_the_modified_date('U');
          }
        endwhile;
        // format output
        if ( '' == $d )
          $last_update = date(get_option('date_format'), $last_update);
        else
          $last_update = date($d, $last_update);
        // output
        echo $last_update;
      else:
        echo 'No posts.';
      endif;
    }
  5. Saurus
    Member
    Posted 5 years ago #

    What is the point of this? The last post you make automatically goes to the top - or first post - of your site. It gives the date and time of the post. That is exactly what you want to do right there. If the format for the date and time is not exactly as you want it you need only to change the includes on your page.php file.

  6. doppelboxx
    Member
    Posted 5 years ago #

    That's why the function isn't called when_was_the_last_post_added! ;-)
    If you edit an existing post, it doesn't change the order of appearance on your site, but it's well an update (in my eyes). That's why it checks the modified_date of all posts, not only of the last post.

  7. doppelboxx
    Member
    Posted 5 years ago #

    My function above didn't do exactly what it was supposed to do and was too complicated anyway. Here's the update:

    /* site_last_updated
    *
    * This will check the modified_date of all posts and give you the most recent date.
    *
    */
    function site_last_updated($d = '') {
    	$allposts = new WP_Query();
    	$allposts->query('orderby=modified&order=ASC');
    	if ( $allposts->have_posts() ) {
    		$allposts->the_post();
    		$last_update = the_modified_date($d);
    		echo $last_update;
    	}
    	else
    		echo 'No posts.';
    }
  8. Digi-junk
    Member
    Posted 5 years ago #

    Hi,

    I'd was looking for something like this but I am new to WordPress and I am not that sure about how to use it.

    I pasted the function into the functions.php file.
    Is that the thing to do?

    How do I call this function in my text to make the last updated date appear there?

    TIA

  9. doppelboxx
    Member
    Posted 5 years ago #

    Hi Digi,

    I pasted the function into the functions.php file.
    Is that the thing to do?

    Yes! Although you'll have to update it with the version below.

    How do I call this function in my text to make the last updated date appear there?

    You have to put this in your template at the desired place:
    <?php site_last_updated('') ?>

    And finally a version of my funtion, that actually does it's job properly:

    /* site_last_updated
    *
    * This will check the modified_date of all published posts and give you the most recent date.
    *
    */
    function site_last_updated($d = '') {
    	$recent = new WP_Query("showposts=1&orderby=modified&post_status=publish");
    	if ( $recent->have_posts() ) {
    		while ( $recent->have_posts() ) {
    			$recent->the_post();
    			$last_update = get_the_modified_date($d);
    		}
    		echo $last_update;
    	}
    	else
    		echo 'No posts.';
    }

    The function accepts the standard parameters for date/time-formatting.

  10. Digi-junk
    Member
    Posted 5 years ago #

    Hi,

    thanks for helping me.

    I am trying to use it in a txt widget, unfortunately nothing shows :(

    Now that I have pasted it into the php file of one of the sidebars I am using, it does show.

    Is it impossible to use it in a txt widget? Or should I use another command?

    TIA

  11. doppelboxx
    Member
    Posted 5 years ago #

    Have a look at this plugin, it enables you to include raw php-functions in a widget.

  12. doppelboxx
    Member
    Posted 5 years ago #

  13. Digi-junk
    Member
    Posted 5 years ago #

    Thank you very very much, it works now.

    I am still playing around with the date format, but I am happy it is working so far.

Topic Closed

This topic has been closed to new replies.

About this Topic