Monday, November 30, 2020
Home Blogging How to Display the Latest Sticky Posts in WordPress

How to Display the Latest Sticky Posts in WordPress

WordPress has this very cool function referred to as sticky posts. Consider sticky posts as featured posts on your weblog. If you mark a submit as sticky, it exhibits up above your new posts, however provided that your theme permits it. On this tutorial we are going to present you how you can show the newest sticky posts in WordPress.

Notice: That is an intermediate stage tutorial and requires primary HTML / CSS information + WordPress themes information.

For those who don’t just like the video or want extra directions, then proceed studying.

Very first thing you’ll want to do is copy and paste this code snippet in your theme’s features.php file or in a site-specific plugin.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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');

The code above queries the WordPress database to retrieve the 5 newest sticky posts. It then shows every sticky submit’s title with a hyperlink in a listing format. We’ve got wrapped all that in a operate and created a shortcode.

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

If you want to make use of shortcodes inside a textual content widget, then you will have so as to add this additional line of code in your theme’s features.php or site-specific plugin.

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

This snippet and performance can very nicely be utilized in featured slider, or some other superior function that you simply wish to show in your web site. This snippet is usually geared towards a WordPress web site that has a customized homepage or {a magazine} type look.

You May Also Like : How to Create a Wiki Knowledge Base Using WordPress

That’s all, we hope this text helped you show newest sticky posts in your WordPress weblog. You may additionally wish to checkout our tutorial on how you can add an expiration date to sticky posts in WordPress.

If you liked this article, make sure to hit the bell for getting our latest updates and share this article with your friends in every social media platforms.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

How to Signup for Godaddy Affiliate Program & Start Making Money

GoDaddy is likely one of the well-liked model names out there of area and web-hosting. They are well-liked for a numerous cause, and so they...

How To Start Selling Website on Flippa: A Beginner’s Guide To Success

If you may have been working on-line for lengthy and into Website flipping, Flippa is one identify that you need to have heard of. Flippa is a...

How to Fix Your Connection is Not Private Error (Site Owners Guide)

Are you seeing ‘Your connection is not private’ error in your WordPress web site? This is the error message you’ll see on Google Chrome. The...

How to Fix the WordPress White Page of Death (Step by Step)

The WordPress white page of dying is one of the most widespread WordPress errors. It can also be one of the most irritating ones as...

Recent Comments