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{ ?>
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’.