Obtenir la date de la dernière mise à jour en dehors de la boucle
6 réponses
- votes
-
- 2013-04-14
Ilest difficile de savoir si vous recherchez le dernier articlemis àjour ou la date de la dernièremise àjour d'un articleen particulier.La réponse de @PatJ suppose lapremière.Pourfaire ce dernier:
$qry = new WP_Query(array('p'=>1)); var_dump($qry->posts[0]->post_modified);
Ou ...
$date = $wpdb->get_var("SELECT post_modified FROM {$wpdb->posts} WHERE ID = 1"); var_dump($date);
Bien sûr,vous devezmodifier l'identifiant dumessagepour qu'il corresponde aumessage que vous recherchez.
It is unclear if you are looking for the last updated post or for the last updated date for some particular post. The answer by @PatJ assumes the former. To do the latter:
$qry = new WP_Query(array('p'=>1)); var_dump($qry->posts[0]->post_modified);
Or...
$date = $wpdb->get_var("SELECT post_modified FROM {$wpdb->posts} WHERE ID = 1"); var_dump($date);
Of course you need to change the post ID to match the post you are looking for.
-
- 2013-04-14
Selon lapage Codexpour
get_the_time()
,il doit être utilisé dans The Loop. La différenceentrethe_time()
etget_the_time()
est que l'ancienecho()
est la date,et ce dernier la renvoie.Il y a quelquesfonctions quifont ce queje pense que vous recherchez - obtenir la dernière dateet heure demise àjour d'unmessage:
get_the_modified_time()
etget_the_modified_date()
. Il semble qu'ils doivent également être utilisés dans The Loop .<?php $args = array( 'orderby' => 'post_modified', 'numberposts' => 1, ); $myposts = get_posts( $args ); if( have_posts() ) { while( have_posts() ) { the_post(); $last_update = get_the_modified_date(); } } echo( "Last modified on $last_update." ); ?>
Si vous êtes sûr de devoir êtreen dehors d'uneboucle,vouspouveztoujours utiliser
$wpdb
:<?php global $wpdb; $sql = "SELECT post_modified FROM $wpdb->posts WHERE post_type='post' AND post_status='publish' ORDER BY post_modified DESC LIMIT 1"; $last_update = $wpdb->get_var( $sql ); echo( "Last updated $last_update." ); ?>
According to the Codex page for
get_the_time()
, it needs to be used in The Loop. The difference betweenthe_time()
andget_the_time()
is that the formerecho()
es the date, and the latter returns it.There are a couple functions that do what I think you're looking for -- get the last updated date and time for a post:
get_the_modified_time()
andget_the_modified_date()
. It looks like they too need to be used in The Loop.Here's one way to get the updated date of the most recent post in your site:
<?php $args = array( 'orderby' => 'post_modified', 'numberposts' => 1, ); $myposts = get_posts( $args ); if( have_posts() ) { while( have_posts() ) { the_post(); $last_update = get_the_modified_date(); } } echo( "Last modified on $last_update." ); ?>
If you're sure you need to be outside of any Loops, you can always use
$wpdb
:<?php global $wpdb; $sql = "SELECT post_modified FROM $wpdb->posts WHERE post_type='post' AND post_status='publish' ORDER BY post_modified DESC LIMIT 1"; $last_update = $wpdb->get_var( $sql ); echo( "Last updated $last_update." ); ?>
-
- 2013-09-21
Un ajouttardif,mais l'extrait suivantpeut êtremodifiépour utiliser laplupart desfonctionsen dehors de laboucle:
/** * Returns a post's modified date, formatted according to $format. * @uses the_modified_time() * * @param int $post_id Post ID. * @param string $format Date format Default: "F j, Y". */ function wpse95769_modified_date_by_id( $post_id = 0, $format = "F j, Y" ){ global $post; $post = &get_post( $post_id ); setup_postdata( $post ); $modified_time = get_the_modified_time( $format ); wp_reset_postdata( $post ); return $modified_time; }
A late addition, but the following snippet can be modified to use most functions outside of the loop:
/** * Returns a post's modified date, formatted according to $format. * @uses the_modified_time() * * @param int $post_id Post ID. * @param string $format Date format Default: "F j, Y". */ function wpse95769_modified_date_by_id( $post_id = 0, $format = "F j, Y" ){ global $post; $post = &get_post( $post_id ); setup_postdata( $post ); $modified_time = get_the_modified_time( $format ); wp_reset_postdata( $post ); return $modified_time; }
-
C'estintéressant;alors commenttransformer celaen un shortcode àmettre dans l'éditeur depagepour afficher la dernière date demodification d'unepage?J'aiessayé de letransformeren shortcode sans succès `function last_modified_shortcode () { function wpse95769_modified_date_by_id ($page_id=0,$format="Fj,Y") { global $page; $page=&get_post ($page_id); setup_postdata ($page); $modified_time=get_the_modified_time ($format); wp_reset_postdata ($page); return $ heure_modifiée; } } add_shortcode ('last_modifed','last_modifed_shortcode'); 'Ths is interesting; so how would one turn this into a shortcode to put into the page editor to show the last modified date of a page? I tried turning it into a shortcode with no luck `function last_modified_shortcode() { function wpse95769_modified_date_by_id( $page_id = 0, $format = "F j, Y" ){ global $page; $page = &get_post( $page_id ); setup_postdata( $page ); $modified_time = get_the_modified_time( $format ); wp_reset_postdata( $page ); return $modified_time; } } add_shortcode( 'last_modifed', 'last_modifed_shortcode' );'
- 0
- 2016-12-04
- BlueDogRanch
-
- 2014-01-29
Un autre ajouttardif,quipeut être utilepour savoir si quelqu'un cherche.Vouspouvez utiliser ces deuxfonctionspour obtenir la date depublicationet la date demodificationen dehors de laboucle.
<?php get_post_time( $d, $gmt, $post, $translate ); ?>
et
<?php get_post_modified_time( $d, $gmt, $post, $translate ); ?>
Vouspouvezen savoirplus sur ces deuxfonctions dans le codex.
http://codex.wordpress.org/Template_Tags/get_post_time http://codex.wordpress.org/Function_Reference/get_post_modified_time
Another late addition, which may be helpful to know if anyone is looking. You can use these two functions to get the post date and modified date outside the loop.
<?php get_post_time( $d, $gmt, $post, $translate ); ?>
and
<?php get_post_modified_time( $d, $gmt, $post, $translate ); ?>
You can read more about these two functions in the codex.
http://codex.wordpress.org/Template_Tags/get_post_time http://codex.wordpress.org/Function_Reference/get_post_modified_time
-
- 2015-12-24
Celapeutne pas être clair d'après les réponsesprécédentes,maisget_post_time ()et get_post_modified_time ()peuvent recevoir un objet ou un ID depublication.Donc,pour obtenir les datespubliéeset modifiéespar ID depublicationen dehors de laboucle:
$published_time = get_post_time( $date_fmt, null, $post_id ); $modified_time = get_post_modified_time( $date_fmt, null, $post_id );
js.
It may not be clear from previous answers, but get_post_time() and get_post_modified_time() can be given a post object or post ID. So, to get the published and modified dates by post ID outside the loop:
$published_time = get_post_time( $date_fmt, null, $post_id ); $modified_time = get_post_modified_time( $date_fmt, null, $post_id );
js.
-
- 2014-02-04
Qu'enest-il de lafonction
get_lastpostmodified( $timezone )
?What about the
get_lastpostmodified( $timezone )
function?-
Cela renvoie la date à laquelle le derniermessage a étémodifié.Cettefonctionn'est doncpas spécifique à un articlemais spécifique aublog -et doncpas à ce qui était demandé.This returns the date when the last post has been modified. So this function is not post-specific but blog-specific - and hence not what was asked for.
- 2
- 2014-02-04
- tfrommen
-
Comment diriez-vous à `get_lastpostmodified` quelmessage vous voulez?How would you tell `get_lastpostmodified` which post you want?
- 0
- 2014-02-04
- Tom J Nowell
J'essaie de comprendre comment afficher la date à laquelle unmessage a étémis àjourpour la dernièrefoisen dehors de laboucle.Je suis capable d'afficher la date depublicationen utilisant
get_the_time()
maisilne semblepas y avoir defonction "sansboucle"pour obtenir la date de la dernièremise àjour.