Top Ad unit 728 × 90

Display Latest Sticky Posts in WordPress


Assuming that you have already created a custom page template and/or already have The Loop ready, paste the following code before the loop.
01<?php
02/* Get all sticky posts */
03$sticky = get_option( 'sticky_posts' );
04
05/* Sort the stickies with the newest ones at the top */
06rsort( $sticky );
07
08/* Get the 5 newest stickies (change 5 for a different number) */
09$sticky array_slice$sticky, 0, 5 );
10
11/* Query sticky posts */
12query_posts( array'post__in' => $sticky'caller_get_posts'=> 1 ) );
13?>
This code can very well be used in featured slider, or any other advanced feature that you would like to display on your site. This snippet is mostly geared toward a WordPress site that has a custom homepage or a magazine style look.
The credit to this code goes to Justin Tadlock and partially to Nathan Rice for coming up with the array slice solution.
WordPress has this very cool feature called sticky posts. Think of sticky posts as featured posts for your blog. When you mark a post as sticky, it shows up above your new posts, but only if your theme permits it. In this tutorial we will show you how to display the latest sticky posts in WordPress.
Note: This is an intermediate level tutorial and requires basic HTML / CSS knowledge + WordPress themes knowledge.

If you don’t like the video or need more instructions, then continue reading.
First thing you need to do is copy and paste this code snippet in your theme’sfunctions.php file or in a site-specific plugin.
01function wpb_latest_sticky() {
02
03/* Get all sticky posts */
04$sticky = get_option( 'sticky_posts' );
05
06/* Sort the stickies with the newest ones at the top */
07rsort( $sticky );
08
09/* Get the 5 newest stickies (change 5 for a different number) */
10$sticky array_slice$sticky, 0, 5 );
11
12/* Query sticky posts */
13$the_query new WP_Query( array'post__in' => $sticky,'ignore_sticky_posts' => 1 ) );
14// The Loop
15if $the_query->have_posts() ) {
16    $return .= '<ul>';
17    while $the_query->have_posts() ) {
18        $the_query->the_post();
19        $return .= '<li><a href="' .get_permalink(). '" title="'  . get_the_title() . '">' . get_the_title() . '</a><br />' . get_the_excerpt(). '</li>';
20         
21    }
22    $return .= '</ul>';
23     
24else {
25    // no posts found
26}
27/* Restore original Post Data */
28wp_reset_postdata();
29
30return $return;
31
32}
33add_shortcode('latest_stickies''wpb_latest_sticky');
The code above queries the WordPress database to retrieve the 5 latest sticky posts. It then displays each sticky post’s title with a link in a list format. We have wrapped all that in a function and created a shortcode.
Now to display your latest sticky posts, you can use the shortcode [latest_stickies] in any WordPress post, page, or even a text widget.
If you would like to use shortcodes inside a text widget, then you will need to add this extra line of code in your theme’s functions.php or site-specific plugin.
1add_filter('widget_text''do_shortcode');
This snippet and function can very well be used in featured slider, or any other advanced feature that you would like to display on your site. This snippet is mostly geared toward a WordPress site that has a custom homepage or a magazine style look.
That’s all, we hope this article helped you display latest sticky posts on your WordPress blog. You may also want to checkout our tutorial on how to add an expiration date to sticky posts in WordPress.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Google+.
Display Latest Sticky Posts in WordPress Reviewed by Unknown on 1:39 AM Rating: 5

No comments:

All Rights Reserved by Boca Raton Real Estate © 2014 - 2015
Powered By Blogger, Designed by Sweetheme

Contact Form

Name

Email *

Message *

Powered by Blogger.