Comment obtenir la date de publication de l'article en dehors de la boucle?
4 réponses
- votes
-
- 2012-06-19
get_the_date
doit être utilisé dans laboucle.En dehors de laboucle,utilisezget_the_time
.$posts = get_posts(array('numberposts'=>-1)); //Get all published posts foreach ($posts as $post){ echo get_the_time('Y-m-d', $post->ID); //Echos date in Y-m-d format. }
Pensez à remplacer
'Y-m-d'
dans cetexempleparget_option('date_format')
car cela affichera la date selon votreparamètre deformat de date dans wp-admin.get_the_date
must be used inside the Loop. For outside the loop useget_the_time
.$posts = get_posts(array('numberposts'=>-1)); //Get all published posts foreach ($posts as $post){ echo get_the_time('Y-m-d', $post->ID); //Echos date in Y-m-d format. }
Consider replacing
'Y-m-d'
in this example withget_option('date_format')
as this will display the date as per your date format setting in wp-admin.-
J'ai aussiessayéget_the_time,maisje me suis quandmêmetrompé de date.I tried get_the_time as well, but still give me the wrong date.
- 0
- 2012-06-19
- dev-jim
-
Et vous lui donnez le codepostal?`get_the_time`est lafonction correcte à utiliserici.And you give pass the post ID to it? `get_the_time` is the correct function to use here.
- 1
- 2012-06-19
- Stephen Harris
-
Deplus,vousne devriezpas utiliser de requête SQLpersonnaliséeici.Utilisez [`get_posts`] (http://codex.wordpress.org/Template_Tags/get_posts)puis si vous souhaitezextraire uniquement les ID,utilisez [` wp_list_pluck`] (http://codex.wordpress.org/Function_Reference/wp_list_pluck)Also - you shouldn't be using a custom SQL query here. Use [`get_posts`](http://codex.wordpress.org/Template_Tags/get_posts) and then if you want to extract just the IDs, use [`wp_list_pluck`](http://codex.wordpress.org/Function_Reference/wp_list_pluck)
- 1
- 2012-06-19
- Stephen Harris
-
Jefais quelque chosepourmettre àjour lepost_statusen fonction de la date depublication,existe-t-il un autremoyen demettre àjour le statut?Jusqu'àprésent,les requêtes SQLme semblent être lemoyen leplus simple.I am doing something to update the post_status based on the publish date, is there any other way to update the status? So far, SQL query seem to easiest way to me.
- 0
- 2012-06-19
- dev-jim
-
Voir lesmodifications - vousne devriezpresquejamais avoirbesoin d'interroger directement labase de données.See edits - you should almost never need to query the database directly.
- 0
- 2012-06-19
- Stephen Harris
-
Je vois que vouspointez.Je l'aiessayéet celafonctionneparfaitement.Maisj'aime savoirpourquoije ne devraispas utiliser directement labase de données?Pour des raisons de sécurité?I see you point. I tried it, and it works perfectly. But I like to know why I shouldn't use query the database directly? For security sake?
- 0
- 2012-06-19
- dev-jim
-
Sécurité (en quelque sorte),performances (probablement)maisprincipalement simplicité du codeet compatibilité ascendante - c'est-à-direen utilisant l'APIfournie (`get_post`) dans ce cas.Security (kind of), performance (probably) but mainly code simplicity and forward-compatibility - i.e. using the provided API (`get_post`) in this case.
- 0
- 2012-06-19
- Stephen Harris
-
Merci Stephen,je ne savaispas qu'ilpouvaitpasser un ID> Lors de l'utilisation deget_posts,iln'y a aucunmoyen d'afficher la date depublication sanselle.Thanks Stephen, I was not aware that could pass an ID> When using get_posts there is no way to display the post date without it.
- 0
- 2013-06-29
- Diana
-
@StephenHarrispendant que cettefonction obtient la date de création de l'article,est-ilpossible d'obtenir la date/heuremodifiéeen dehors de laboucle?@StephenHarris while this function gets the date the post was created, is it possible to get the modified date/time outside the loop?
- 0
- 2013-09-20
- Anagio
-
@Anagio - voir http://wordpress.stackexchange.com/questions/95769/get-date-of-last-update-outside-of-loop/115044#115044@Anagio - see http://wordpress.stackexchange.com/questions/95769/get-date-of-last-update-outside-of-loop/115044#115044
- 0
- 2013-09-21
- Stephen Harris
-
Selon le Codex,labalise `get_the_time` doit être utilisée dans The Loop.http://codex.wordpress.org/Function_Reference/get_the_timeAccording to the Codex, `get_the_time` tag must be used within The Loop. http://codex.wordpress.org/Function_Reference/get_the_time
- 0
- 2014-01-14
- Maor Barazany
-
@MaorBarazany,semble que le codexestfaux,voir la source: https://core.trac.wordpress.org/browser/tags/3.8/src/wp-includes/general-template.php#L1497@MaorBarazany, seems codex is wrong, see source: https://core.trac.wordpress.org/browser/tags/3.8/src/wp-includes/general-template.php#L1497
- 0
- 2014-01-14
- Stephen Harris
-
Vous avez lié àget_post_timeplutôt qu'àget_the_time,ce quiest assez déroutant.Ils accomplissent lamême chosemais ont des arguments différents.Avez-vous une chance de réparer le lien?Merci!You linked to get_post_time rather than get_the_time, which is rather confusing. They accomplish the same thing but have different args. Any chance you could fix the link? Thanks!
- 0
- 2014-06-12
- David Hobs
-
@DavidHobs Fixe :)@DavidHobs Fixed :)
- 1
- 2014-06-13
- Stephen Harris
-
- 2018-03-26
Quelques soultionsmodernes
Première solution
<?php echo get_the_date('j F Y', get_the_ID()) ?>
Solution deux
<?php the_time(get_option('date_format')) ?>
Some Modern soultions
Solution One
<?php echo get_the_date('j F Y', get_the_ID()) ?>
Solution Two
<?php the_time(get_option('date_format')) ?>
-
- 2018-10-12
Vouspouvez utiliser get_post () ou get_post_field () pour cela,les deuxfonctionnenten dehors de laboucle.
$post_object = get_post($id); $post_date = date( 'F jS, Y', strtotime( $post_object->post_date ) );
Une liste complète des valeurs renvoyéesparget_post:
WP_Post Object ( [ID] => [post_author] => [post_date] => [post_date_gmt] => [post_content] => [post_title] => [post_excerpt] => [post_status] => [comment_status] => [ping_status] => [post_password] => [post_name] => [to_ping] => [pinged] => [post_modified] => [post_modified_gmt] => [post_content_filtered] => [post_parent] => [guid] => [menu_order] => [post_type] => [post_mime_type] => [comment_count] => [filter] => )
You can use get_post() or get_post_field() for this, both work outside the loop.
$post_object = get_post($id); $post_date = date( 'F jS, Y', strtotime( $post_object->post_date ) );
A full list of values returned by get_post:
WP_Post Object ( [ID] => [post_author] => [post_date] => [post_date_gmt] => [post_content] => [post_title] => [post_excerpt] => [post_status] => [comment_status] => [ping_status] => [post_password] => [post_name] => [to_ping] => [pinged] => [post_modified] => [post_modified_gmt] => [post_content_filtered] => [post_parent] => [guid] => [menu_order] => [post_type] => [post_mime_type] => [comment_count] => [filter] => )
-
- 2012-06-19
essayez comme ça
$getPosts = $wpdb->get_results( " SELECT ID, post_date,post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY ID ASC " ); foreach ( $getPosts as $myPost ) { $id = $myPost->post_date; echo $myPost->ID.' | '. $myPost->post_title.' | '. get_the_date("Y-m-d",$id ).'<br />'; }
modifier
get_the_time Renvoie l'heure dumessage actuel à utiliser dans PHP. Iln'affichepas l'heure. Pour afficher l'heure d'unmessage,utilisezthe_time (). Cettebalise doit être utilisée dans The Loop.
get_the_date Labalise demodèleget_the_date récupère la date à laquelle le $ actuel lemessage a été écrit. Contrairement àthe_date (),cettebalise retourneratoujours la date. Modifiez la sortie avec lefiltre «get_the_date».
Est-ce queje manque quelque choseici?
try like this
$getPosts = $wpdb->get_results( " SELECT ID, post_date,post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY ID ASC " ); foreach ( $getPosts as $myPost ) { $id = $myPost->post_date; echo $myPost->ID.' | '. $myPost->post_title.' | '. get_the_date("Y-m-d",$id ).'<br />'; }
edit
get_the_time Returns the time of the current post for use in PHP. It does not display the time. To display the time of a post, use the_time(). This tag must be used within The Loop.
get_the_date The get_the_date template tag retrieves the date the current $post was written. Unlike the_date() this tag will always return the date. Modify output with 'get_the_date' filter.
Am I missing something here?
-
Veuillezfusionner vos réponses.Please merge your answers.
- 0
- 2012-06-19
- fuxia
-
montre-moi commentfusionner,s'ilte plaît?show me how to merge, please?
- 0
- 2012-06-19
- Gembel Intelek
-
Modifiez lapremière réponseet copiez-y le code de la seconde.Puis supprimez le second.Edit the first answer and copy the code from the second into it. Then delete the second.
- 0
- 2012-06-19
- fuxia
-
J'aiessayé vos codeset jepeux obtenir la valeur deget_the_time,pas deget_the_date.I tried your codes, and I can get the value from get_the_time, not get_the_date.
- 0
- 2012-06-19
- dev-jim
Je dois retirer la date depublication dumessagepour que lemessageexpire automatiquement.Leproblèmeest queje neparvienspas à obtenir labonne date depublication.
Voicimon code:
Lorsqueje fais écho à la $postdate,la dateesterronée.Pas la date quiexiste dans latable wp_posts.
Commentpuis-je obtenir la date correctement?