fbpx

Create a Custom Category Based Recent Posts Sidebar in Genesis

by | Jan 22, 2013

Wordpress-Web-Design-Hack-How-To-Create-a-Custom-Category-Based-Recent-Posts-Sidebar-in-Genesis

WordPress Webdesign Hack: Create a Custom Category Based Recent Posts Sidebar in Genesis

While the ‘Recent Posts’ widget works for most websites, it can be a little limiting when you want to display posts from only one category.

With Genesis, adding in a custom category based recent posts sidebar is relatively easy.

Here is how to do it.

/*Add recent posts in sidebar for web design category*/
add_action( 'genesis_after_sidebar_widget_area', 'add_recent_webdesign'); 

function add_recent_webdesign(){
if ( !is_singular( 'post' ) ){  return;}
else{
?>

Latest Web Design Articles

    have_posts()) : $recent->the_post();?>

Code Breakdown

The ‘add_action..’ line tells Genesis to add something after the Genesis sidebar widget area, which is your Primary Sidebar.

That something is the function ‘add_recent_webdesign()’.

The function says, if the page being is not a single post, then do nothing.

If it is a single post, then add the following html / php code.

The code adds a div of class ‘widget’, with the heading of ‘Latest Web Design Articles’.

Then it runs a WordPress loop query which says return posts where the category_name is ‘web-design’ and shows a maximum of 5 posts.

Then it adds each post title as a list item and links it to the post.

That’s it.

Further Website Customisation

If you want to customise the loop above, here are your options.

If you want to display your custom recent posts sidebar on top of the Primary Sidebar, change the ‘genesis_after_sidebar_widget_area’ to ‘genesis_before_sidebar_widget_area’.

If you want to display more than 5 posts for that category, or less for that matter, change the ‘showposts=5’ to ‘showposts=8’ or ‘showposts=3’.

Subscribe to Keep Up With Our
Guides & Insights!

About the Author - 3am Ideas

3am Ideas is digital marketing agency focussing on providing data-driven services that get measurable and trackable results.

Based in Perth, Western Australia, we've been looking after small and medium-sized businesses since 2014, having worked with clients all over the country.

Our aim is to adopt the role of Virtual Marketing Officer for you, becoming part of your team, sharing in the successes, the blood, sweat and tears.

We're here for you. Let's get the job done!