How to Display the Latest Sticky Posts in WordPress

WordPress has this very cool function referred to as sticky posts. Think of sticky posts as featured posts to your weblog. When you mark a publish as sticky, it presentations up above your new posts, however provided that your theme allows it. In this educational we can display you the way to show the newest sticky posts in WordPress.

Note: This is an intermediate stage educational and calls for fundamental HTML / CSS wisdom + WordPress issues wisdom.

Video Tutorial

If you don’t like the video or want extra directions, then proceed studying.

First factor you wish to have to do is replica and paste this code snippet in your theme’s purposes.php document or in a site-specific plugin.



serve as wpb_latest_sticky() { 

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

/* Sort the stickies with the latest ones at the best */
rsort( $sticky );

/* Get the Five latest stickies (exchange Five for a distinct quantity) */
$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() ) {
	$go back .= '<ul>';
	whilst ( $the_query->have_posts() ) {
		$the_query->the_post();
		$go back .= '<li><a href="' .get_permalink(). '" name="'  . get_the_title() . '">' . get_the_title() . '</a><br />' . get_the_excerpt(). '</li>';
		
	}
	$go back .= '</ul>';
	
} else {
	// no posts discovered
}
/* Restore authentic Post Data */
wp_reset_postdata();

go back $go back; 

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

The code above queries the WordPress database to retrieve the Five newest sticky posts. It then presentations every sticky publish’s name with a hyperlink in a listing layout. We have wrapped all that in a serve as and created a shortcode.

Now to show your newest sticky posts, you’ll use the shortcode [latest_stickies] in any WordPress publish, web page, or perhaps a textual content widget.

If you desire to to use shortcodes inside of a textual content widget, then you’re going to want to upload this additional line of code in your theme’s purposes.php or site-specific plugin.


add_filter('widget_text', 'do_shortcode');

This snippet and serve as can rather well be used in featured slider, or every other complex function that you desire to to show in your website. This snippet is most commonly aimed toward a WordPress website that has a customized homepage or {a magazine} taste glance.

That’s all, we are hoping this newsletter helped you show newest sticky posts in your WordPress weblog. You might also need to checkout our instructional on how to upload an expiration date to sticky posts in WordPress.

If you loved this newsletter, then please subscribe to our YouTube Channel for WordPress video tutorials. You too can to find us on Twitter and Google+.

Recent Articles

How to Auto-Apply Coupons in WooCommerce Using Coupon URLs

Do you wish to have to give your WooCommerce consumers a distinct hyperlink that mechanically applies a chit for them? Normally, customers want to upload...

How to Add Multiple Authors (Co-Authors) for Posts in WordPress

Do you wish to have to display more than one authors for a publish in WordPress? Many web pages frequently have more than one...

How to Restrict WordPress Site Access by IP or Logged In Users

You are about to release a brand new product, and you have got evolved an ideal web page for it the use of the...

How to Allow Users to Post Anonymous Comments in WordPress

Recently one in all our customers requested if it used to be imaginable to permit nameless feedback in WordPress. By default, customers can not...

How to Get Email Alerts for 404 Errors in WordPress

404 mistakes are a reason of shock for many WordPress customers. Particularly those that have simply moved from WordPress.com to self hosted WordPress.org, or...

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox