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 les dernières publications épinglées 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.

WordPress dispose d’une fonctionnalité très intéressante appelée  » publications épinglées ». Les publications épinglées sont en quelque sorte les mises en avant de votre blog. Lorsque vous marquez une publication comme épinglée, elle s’affiche au-dessus de vos nouveaux articles, mais uniquement si votre thème le permet. Dans ce tutoriel, nous allons vous afficher les dernières publications épinglées dans WordPress.

Latest Sticky Posts Demo

Note : Ce tutoriel est de niveau intermédiaire et nécessite des connaissances de base en HTML / CSS + des connaissances sur les thèmes WordPress.

Tutoriel vidéo

Subscribe to WPBeginner

Si vous n’aimez pas la vidéo ou si vous avez besoin de plus d’instructions, alors continuez à lire.

La première chose à faire est de copier et de coller cet extrait de code dans le fichier functions.php de votre thème ou dans une extension spécifique à votre site.


function wpb_latest_sticky() { 

/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );

/* Sort the stickies with the newest ones at the top */
rsort( $sticky );

/* Get the 5 newest stickies (change 5 for a different number) */
$sticky = array_slice( $sticky, 0, 5 );

/* Query sticky posts */
$the_query = new WP_Query( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ) );
// The Loop
if ( $the_query->have_posts() ) {
	$return .= '<ul>';
	while ( $the_query->have_posts() ) {
		$the_query->the_post();
		$return .= '<li><a href="' .get_permalink(). '" title="'  . get_the_title() . '">' . get_the_title() . '</a><br />' . get_the_excerpt(). '</li>';
		
	}
	$return .= '</ul>';
	
} else {
	// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

return $return; 

} 
add_shortcode('latest_stickies', 'wpb_latest_sticky');

Le code ci-dessus interroge la base de données de WordPress pour récupérer les 5 dernières publications épinglées. Il affiche ensuite le titre de chaque publication épinglée avec un lien dans un format de publication. Nous avons enveloppé tout cela dans une fonction et créé un code court.

Maintenant, pour afficher vos derniers articles épinglés, vous pouvez utiliser le code court [latest_stickies] dans n’importe quelle publication WordPress, page, ou même dans un widget texte.

Si vous souhaitez utiliser des codes courts à l’intérieur d’un widget texte, vous devrez ajouter cette ligne de code supplémentaire dans le fichier functions.php de votre thème ou dans le plugin spécifique à votre site.

add_filter('widget_text', 'do_shortcode');

Cet extrait et cette fonction peuvent très bien être utilisés dans un Diaporama, ou toute autre fonctionnalité avancée que vous souhaiteriez afficher sur votre site. Cet extrait est surtout destiné aux sites WordPress qui ont une page d’accueil personnalisée ou un style magazine.

C’est tout, nous espérons que cet article vous a aidé à afficher les dernières publications épinglées sur votre blog WordPress. Vous pouvez également consulter notre module sur la façon d’ajouter une date d’expiration aux publications épinglées sur WordPress.

Si vous avez aimé cet article, veuillez vous abonner à notre chaîne YouTube pour des tutoriels vidéo sur WordPress. Vous pouvez également nous trouver sur Twitter et Google+.

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

9 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. umair says

    very informative efforts made by you, this code works perfectly fine,
    i have a question that i have multiple categories & sub-categories, so i just want to make a sticky post to each category separately, and displayed on category list page where all posts of that specific category listed, like (Sports category->one sticky post – cooking recipes category -> one sticky post) etc

  3. Dom says

    Thanks for getting me on the right track!!
    The code works great in getting the Title to show up where the short code is pasted.

    However, I was trying to find a method to display the entire post (all content, in stead of just a title or even excerpt) on a specific page.

    I decided to play with your code above in order to try and achieve this and finally managed…
    Although the title displays the same size as paragraph text, I’m sure I can fix that.

    Here’s what I changed…please let me know if you think there’s something I should reconsider lest the whole world explodes ;)

    I simply changed your get_the_excerpt() text to get_the_content() and this successfully pulled in the entire blog post into that page.
    In order to remove the bulleted indentation I then removed the  »  »  »  » tags, but had to leave the apostrophes in place, otherwise the page wouldn’t load.
    So in essence, where  » or  » was before, now there is only  » and so on.

    That’s all I changed and it mostly works like a charm for me…
    There are 2 issues I noticed:
    1: Sometimes it loads most of the post, but not always right down to the end, especially if the post is edited afterwards…anyone with suggestions to ensure this doesn’t happen?
    2: I’d love if the featured image would also show up, this would make it the ultimate flexible solution! Any thoughts here?

    Thanks again for the advice!

    Hope what I explained makes sense to anyone who wants to achieve the same as I was after.

  4. richard says

    Hello

    Thank you for your code. However i try to achieve something and i have 2 issues.

    I want to display only one sticky post on my home page and only the youtube video present on the content.

    ACtually i display the title and all the content of a post (include texts, pictures and videos).
    Second problem is that it display youtube as a link and not embed directly the video into my homepage.

    Thank you for your help

  5. Raviraj says

    Hi,

    I have tried out with the code, It works fine when at least 1 post has been assigned as sticky post else when none of the posts are assigned a sticky, then it will display all latest 5 posts.

    So would be the condition to display only sticky posts, if there are no sticky posts assigned then It should not display anything.

    Thanks in advance

  6. AnastisSourgoutsidis says

    ‘caller_get_posts’ has been replaced by ‘ignore_sticky_posts’ since v3.1 so I think its important to change your code to reflect that.
    Also, a) $post->ID is not needed in this instance of get_permalink() as you’ve setup the post data by calling $the_query->the_post(), and if for whatever reason you still want to include $post->ID, you should be global-ing it, i.e. global $post;

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.