WordPress dispone di una funzione molto interessante chiamata sticky post. Pensate ai post appiccicati come ai post in evidenza del vostro blog. Quando si contrassegna un post come appiccicoso, questo viene visualizzato sopra i nuovi post, ma solo se il tema lo consente. In questo tutorial vi mostreremo come visualizzare gli ultimi post appiccicati in WordPress.
Nota: questo è un tutorial di livello intermedio e richiede una conoscenza di base di HTML e CSS e dei temi di WordPress.
Video tutorial
Se il video non vi piace o avete bisogno di ulteriori istruzioni, continuate a leggere.
La prima cosa da fare è copiare e incollare questo frammento di codice nel file functions.php del vostro tema o in un plugin specifico del sito.
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');
Il codice qui sopra interroga il database di WordPress per recuperare i 5 post appiccicosi più recenti. Quindi visualizza il titolo di ciascun post con un link in un elenco. Abbiamo racchiuso il tutto in una funzione e creato uno shortcode.
Ora, per visualizzare gli ultimi post appiccicosi, è possibile utilizzare lo shortcode [latest_stickies] in qualsiasi post, pagina o widget di testo di WordPress.
Se desiderate utilizzare gli shortcode all’interno di un widget di testo, dovrete aggiungere questa riga di codice supplementare nel functions.php del vostro tema o nel plugin specifico del sito.
add_filter('widget_text', 'do_shortcode');
Questo snippet e questa funzione possono essere utilizzati benissimo per il featured slider o per qualsiasi altra caratteristica avanzata che si desidera visualizzare sul proprio sito. Questo snippet è orientato soprattutto a un sito WordPress che ha una homepage personalizzata o un aspetto in stile rivista.
Questo è tutto, speriamo che questo articolo vi abbia aiutato a visualizzare gli ultimi post appiccicosi sul vostro blog WordPress. Potreste anche consultare il nostro tutorial su come aggiungere una data di scadenza ai post appiccicosi in WordPress.
Se questo articolo vi è piaciuto, iscrivetevi al nostro canale YouTube per i video tutorial su WordPress. Potete trovarci anche su Twitter e Google+.
umair
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
Dom
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.
Achi Baten
How can i show homepage post like you?
WPBeginner Support
Our site uses a custom theme designed specifically for our own needs. However, you can find similar functionality in some magazine themes out there.
Admin
richard
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
Raviraj
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
WPBeginner Staff
Thanks for pointing this out. We have updated the code.
AnastisSourgoutsidis
‘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;