WordPress.org

Ready to get started?Download WordPress

Forums

How to... Last Updated text (9 posts)

  1. Dickie
    Member
    Posted 8 years ago #

    Hi,
    I was wondering if there was an easy way to get the last update that was made to WordPress.
    I am using WP as a CMS and would like to add a "Site last Updated" tag to the front page (well all pages really)

    Does anyone know of a plugin that does this already or how to work out this information so I can write the plugin myself.

    Thanks

    Dickie

  2. Kafkaesqui

    Posted 8 years ago #

    Hey, any chance I've sent you an email on this topic?

    :)

  3. Kafkaesqui

    Posted 8 years ago #

    If not, and for public consumption, the contents of that email:

    Use this in your templates where you want to display the last update to your blog:

    <?php
    $last = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts order by post_modified DESC LIMIT 1");
    echo "Last update: " . mysql2date(get_settings('date_format'), $last);
    ?>

    Description of code:
    $last receives the modified date for the last post or Page on your blog. The echo line displays the modified date, filtered through the WP mysql2date() function (passing it the default date format on your blog).

    If you want to make sure to select only Page updates, use this for your $last code:

    $last = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_status = 'static' order by post_modified DESC LIMIT 1");

    You can customize the date/time format by using PHP date format strings:

    echo echo "Last update: " . mysql2date('F j, Y @ g:i a', $last);

    This will output:

    Last update: March 6, 2006 @ 11:09 pm

    Also see:
    http://codex.wordpress.org/Formatting_Date_and_Time

  4. Dickie
    Member
    Posted 8 years ago #

    No you hadn't sent that to me by email, but thankyou very much, that's working a treat

    Just a point I needed to add the global line to the start of the function, but other than that cool, thanks.

    <?php
    global $wpdb;
    $last = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts order by post_modified DESC LIMIT 1");
    echo "Last update: " . mysql2date(get_settings('date_format'), $last);
    ?>

  5. aketus
    Member
    Posted 8 years ago #

    Careful if you copy Kaf's php date/time example there - there shouldn't be two 'echo's in the code - that gave me errors the first time until I paid more attention and realised what the problem was.

  6. Kafkaesqui

    Posted 8 years ago #

    Damn copy/paste, my constant nemesis...

  7. linus587
    Member
    Posted 8 years ago #

    Is there a way I could use this code to pull the same information, but for a posts under a particular category? Is there some additional code I could add for example, to limit this function to only look at posts under category ID 6?

    TIA

  8. seanamcdonald
    Member
    Posted 8 years ago #

    i made up a new template with that code in it so that it would display the last time that page was modified... but it always displays todays date.

    is that because it is in the template and displaying the last time the template was adjusted, or is there something in the code that makes it displays that day's date?

  9. ninjaboy
    Member
    Posted 7 years ago #

    <?php
    $lastpageupdate = $post->post_modified;
    $lastsiteupdate = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts order by post_modified DESC LIMIT 1");

    echo "Page last edited on: " . mysql2date(get_settings('date_format'), $lastpageupdate);
    echo " Site last updated on: " . mysql2date(get_settings('date_format'), $lastsiteupdate);
    ?>

    This outputs:
    Page last edited on: December 31, 2006 Site last updated on: January 6, 2007

    This worked fine for me in my footer, I didn't need "global $wpdb;" in line 2 as in some code I have seen, although this may be down to my server config.

    I have been reading around this subject for a while and there are various posts on the subject around this site and the net. This all in-one solution works fine for me in my footer (outside the loop) and I hope it helps a few people out!

    You could always use the post-updated plugin if you dont want to edit your template files, or change them regularly - it's over at http://guff.szub.net/2005/02/22/post-updated/ but I wanted more control over the formating of the output without hacking into the plugin.

Topic Closed

This topic has been closed to new replies.

About this Topic