How to Display the Latest Sticky Posts in WordPress


How to Display the Latest Sticky Posts in WordPress
Ebony Austin

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?

Menu
Blanche Gordon

01 function 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.
07 * Remove this if you want to display oldest posts first
08 */
09 rsort( $sticky );
10
11 /* Get the 5 newest stickies (change 5 for a different number) */
12 $sticky = array_slice( $sticky, 0, 5 );
13
14 /* Query sticky posts */
15 $the_query = new WP_Query( array( ‘post__in’ => $sticky, ‘caller_get_posts’ => 1 ) );
16
17
18 // The Loop
19 if ( $the_query->have_posts() ) {
20 $return .= ‘<ul>’;
21 while ( $the_query->have_posts() ) {
22 $the_query->the_post();
23 $return .= ‘<li><a href=”‘ .get_permalink($post->ID). ‘” title=”‘ . get_the_title() . ‘”>’ . get_the_title() . ‘</a></li>’;
24
25 }
26 $return .= ‘</ul>’;
27
28 } else {
29 // no posts found
30 }
31
32
33 /* Restore original Post Data */
34 wp_reset_postdata();
35
36 return $return;
37
38 }
39 add_shortcode(‘latest_stickies’, ‘wpb_latest_sticky’);

Display the Latest Sticky Posts in WordPress
Jacob Stone

Have you ever want to display the latest sticky posts as featured posts for your in WordPress? Following our steps in this section.
First of all, you need to copy and paste code snippet as below into your theme’s functions.php file to queries the WordPress database to retrieve the 5 latest sticky posts (each sticky post’s title with a link in a list format)

Be the first to comment

Leave a Reply

Your email address will not be published.


*