Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
Coupe WPB
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

Comment afficher une liste des dernières publications mises à jour dans WordPress

Note éditoriale : Nous percevons une commission sur les liens des partenaires sur WPBeginner. Les commissions n'affectent pas les opinions ou les évaluations de nos rédacteurs. En savoir plus sur Processus éditorial.

Chez WPBeginner, nous affichons la date de dernière modification au lieu de la date de publication originale pour tous nos articles. Nous pensons que c’est une mauvaise idée de retirer les dates de vos publications de blog WordPress. Si vous utilisez la date de dernière modification, alors vous pourriez vouloir afficher une liste de vos publications récemment mises à jour sur votre site. Dans cet article, nous allons vous afficher comment afficher une liste des dernières publications mises à jour dans WordPress.

Chaque fois que vous mettez à jour une publication, WordPress stocke la date et l’heure de cette mise à jour dans la table des publications en tant que date de dernière mise à jour. Nous allons vous afficher comment créer une requête WordPress personnalisée pour lister vos articles les plus récemment mis à jour.

Copiez et collez ce code dans une extension spécifique au site ou dans le fichier functions.php de votre thème.


function wpb_lastupdated_posts() { 

// Query Arguments
$lastupdated_args = array(
'orderby' => 'modified',
'ignore_sticky_posts' => '1'
);

//Loop to display 5 recently updated posts
$lastupdated_loop = new WP_Query( $lastupdated_args );
$counter = 1;
$string .= '<ul>';
while( $lastupdated_loop->have_posts() && $counter < 5 ) : $lastupdated_loop->the_post();
$string .= '<li><a href="' . get_permalink( $lastupdated_loop->post->ID ) . '"> ' .get_the_title( $lastupdated_loop->post->ID ) . '</a> ( '. get_the_modified_date() .') </li>';
$counter++;
endwhile; 
$string .= '</ul>';
return $string;
wp_reset_postdata(); 
} 

//add a shortcode
add_shortcode('lastupdated-posts', 'wpb_lastupdated_posts');

C’est tout. Maintenant, si vous voulez afficher les dernières publications mises à jour dans les fichiers de modèle de votre thème, alors vous pouvez l’utiliser comme ceci :

<?php 
if (function_exists(wpb_lastupdated_posts)) : 
wpb_lastupdated_posts();
endif;
?>

Pour afficher les dernières publications mises à jour dans les articles, pages et widgets de WordPress, vous pouvez utiliser le code court [lastupdated-posts].

Il existe de nombreuses façons de trier vos articles dans WordPress. Outre l’ordre croissant, décroissant et aléatoire, vous pouvez également afficher les publications par date d’expiration. Avec cet article, vous pouvez désormais afficher les publications selon l’heure de la dernière modification.

Comment utiliseriez-vous cette fonction sur votre site ? Affichez-vous la date de publication originale ou la date de dernière modification ? Faites-le nous savoir en laissant un commentaire ci-dessous.

Divulgation : Notre contenu est soutenu par les lecteurs. Cela signifie que si vous cliquez sur certains de nos liens, nous pouvons gagner une commission. Consultez comment WPBeginner est financé, pourquoi cela compte et comment vous pouvez nous soutenir. Voici notre processus éditorial.

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

L'ultime WordPress Toolkit

Accédez GRATUITEMENT à notre boîte à outils - une collection de produits et de ressources liés à WordPress que tous les professionnels devraient avoir !

Reader Interactions

38 commentairesLaisser une réponse

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Thomas Mirke says

    Hi,
    i modified the code from « modified » to « date ». Then i noticed, that $lastupdated_loop->have_posts() is limited to the number of last posts which is presented on the main page. What is to do to get an (independent) number of f.e. 50 last posts? Instead of 6 as stored in the wp settings?

    • WPBeginner Support says

      You should be able to change from line 12 of the code the section ‘&& $counter < 5' and change the 5 to the number of posts you want to show.

      Administrateur

      • Thomas Mirke says

        Thank you, you can be sure, that this was the first thing i did. But notice that when you change the number in your code it is without result. Write f.e. ‘&& $counter <20' – this does not work as there is f.e. 6 in the wordpress settings. So the question is how to overwrite those settings temporarily or to find any other solution that works without corresponding to the max-posts-per-page settings in wordpress.

        • WPBeginner Support says

          With your theme overriding the query that way, you could try adding a comma to the end of line 6 and on the next line add ‘posts_per_page’ => 20 to work on overriding your theme’s posts per page.

    • WPBeginner Support says

      You would want to check with your eCommerce plugin for what options they would recommend as they normally have a widget or similar option.

      Administrateur

  3. Sascha says

    Thank you for the instructions. That was exactly what I was looking for! Finally I can show the latest updates in my sidebar.

  4. Gwénaël says

    Hi,

    It works well, but it does not respect the content permissions of the page. It turns all the content visible to anybody…

  5. Des says

    Hi,

    I followed you ‘How to Create a Custom WordPress Widget’, including ‘Creating a Site-Specific WordPress Plugin’ and that works perfectly. Great tutorial as I now have the ‘Hello world’ text displaying as a sidebar.

    But this one has me completely stumped. I followed the tutorial as best I could but it just displays my ‘Hello world’ text, never any list of updated posts.

    Where in my custom plugin file do I put the function wpb_lastupdated_posts()? Where do I put the ‘add_shortcode’ and where do I put the ‘if (function_exists(wpb_lastupdated_posts)) : ‘ statement?

    Many thanks,
    Des

  6. Tommy says

    Installed the code as stated. When using the short code- I add it to the bottom of the post. but when view the code- it puts the information on the top of the post. weird.

    • Marceli says

      Same request here. I’m interested in listing recently updated Pages + showing the last_modified date. Ideally with some excerpt or number of characters from page.

  7. jerik says

    Great code, but is there anyway to get this exact same function but based on comments made by specific user (admin) and list the most recent comment made as the top of the threads in the list?

    Thanks!

  8. Alberto Aguilar says

    Hi… great post…. Yet I was wondering if you could give me a hand on something: I need to take the picture and title of the last three published posts and arrange them in some kind of Gallery (Just to show the latest three published articles, Any idea if there’s a plugin for that, I assume I can use the code you just placed above, but Im not quite sure on the DB structure)

    Thanks in advance… =)

  9. Pierre Guimond says

    I do not know how to change the office location address on my WordPress web site. Try as I may, I cannot reach it to change it. http://www.mastheadpa.ca I would appreciate simple and straight answer. I can do most of the upkeep but that address change eludes me and i have not seen the key in the WP information and tutorials. help.

    • WPBeginner Support says

      @Pierre your office location is stored inside a widget. Inside your WordPress admin area, go to Appearance » Widgets. On your right hand column you will see a list of widgets currently in use on your site. Look for Footer Widget Area and there you will see Get in Touch widget which you can edit and save your changes.

      Administrateur

  10. Steve says

    Hi, love this article but I don’t quite get what is meant to be updated when you mention ‘use it like this:
    1

    Which file is this?

    Steve

    • WPBeginner Support says

      Some users might want to display last updated posts into different templates of their WordPress themes. Those users can use this code to add it. Other users can use the shortcode to add it in their posts, pages, and widgets.

      Administrateur

  11. erricgunawan says

    Instead of using $counter for your loop, why don’t you just use the 'posts_per_page' attribute on the WP_Query args ?

    Mine goes like this:

    $lastupdated_args = array(
    ‘orderby’ => ‘modified’,
    ‘ignore_sticky_posts’ => 1,
    ‘posts_per_page’ => 5
    );

    Also, when I try the above code, it only give me one last modified post (not five as it should be).
    Wonder why …

  12. Mary says

    I love your articles but this one is way over my head. Are there any plugins for this? :)

    Sorry but I dont know the « innards » of the website and have broken my site when I tried a few things.

    It is a great idea though.Thanks for all you great work. Mary

    • Rudd says

      The code above is actually a ‘plugin’. Simply copy and paste the first code in functionality plugin. Then, there are two ways to display the list of posts, either using template tag or the easiest, using [lastupdated-posts] shortcode.

Laisser une réponse

Merci d'avoir choisi de laisser un commentaire. Veuillez garder à l'esprit que tous les commentaires sont modérés selon notre politique de commentaires, et votre adresse e-mail ne sera PAS publiée. Veuillez NE PAS utiliser de mots-clés dans le champ du nom. Ayons une conversation personnelle et significative.