Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
Featured posts let you choose specific posts to highlight on your webpage. There are several plugins available to achieve featured posts as well.
In this article we'll use a pretty simple approach to add featured posts using Advance Custom Fields Plugin.
Head over to the custom fields page and a new custom field, let's name is Featured Post and set the field type to true/false, set the location to Post & simply publish it.
You can now see a simple check-box on each of your post page. Now let's add some functionality to make it work.
In your index.php or either in your functions.php, however your theme structure is for your Wordpress website, add the following code to get the featured posts.
$featuredPosts = get_posts( array(
'posts_per_page' => 4,
'orderBy' => 'desc',
'meta_query' => array(
array(
'key' => 'featured_post',
'compare' => '=',
'value' => '1'
)
)
) );
You can then pass the featured posts and loop through these to display on your frontend however you want. As simple as it is.
Now to make any of your post a featured post, just check the box next to featured custom field and well that's it. Simple enough, eh?