Latest Tech and Tips

How to Display the Latest Sticky Posts in WordPress

11

WordPress has this extremely trendy attribute called sticky posts. Think of sticky posts as included posts for your blog site. When you note a message as sticky, it turns up over your brand-new posts, yet just if your style allows it. In this tutorial we will certainly reveal you how to display the latest sticky postsin WordPress

Latest Sticky Posts Demo

Note: This is an intermediate degree tutorial and also calls for standard HTML/ CSS expertise + WordPress styles expertise.

Video Tutorial

Subscribe to WPBeginner

If you do not such as the video clip or require even more directions, after that proceed analysis.

First point you require to do is duplicate and also paste this code fragment in your style’s functions.php data or in a site-specific plugin.


feature wpb_latest_sticky() {

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

/ * Sort the stickies with the most recent ones at the leading */.
rsort( $sticky );.

/ * Get the 5 most recent stickies (modification 5 for a various number) */.
$ sticky = array_slice( $sticky, 0, 5 );.

/ * Query sticky posts */.
$ the_query = brand-new WP _Query( variety( 'message __ 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(). '< < br/>>'. get_the_excerpt(). '<';.

}
$ return.= '<';.

} else {
// no posts located.
}
/ * Restore initial Post Data */.
wp_reset_postdata();.

return $return;.

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

The code over inquiries the WordPress data source to get the 5 latest sticky posts. It after that presents each sticky message's title with a web link in a checklist layout. We have actually covered all that in a feature and also produced a shortcode.

Now to display your latest sticky posts, you can utilize the shortcode [latest_stickies] in any kind of WordPress message, web page, or perhaps a message widget.

If you would certainly such as to utilize shortcodes inside a message widget, after that you will certainly require to include this additional line of code in your style's functions.php or site-specific plugin.

add_filter(' widget_text', 'do_shortcode');.

This fragment and also feature can extremely well be utilized in included slider, or any kind of various other innovative attribute that you would certainly such as to display on your website. This fragment is primarily tailored towards a WordPress website that has a personalized homepage or a publication design appearance.

That's all, we wish this short article assisted you display latest sticky posts on your WordPress blog site. You might likewise desire to check out our tutorial on how to include an expiry day to sticky postsin WordPress

If you liked this short article, after that please subscribe to our YouTube Channel for WordPress video clip tutorials. You can likewise discover us on Twitter and alsoGoogle+

.