One consequence of adding a Microblog feed to this site was that a lot of post IDs were showing up in the Recent Posts widget. It turns out that if a post is missing a title, the widget falls back to just displaying the ID number. Sad!
It turns out that customizing the widget was fairly straightforward. Since I already have a child theme set up, all I had to do was copy the widget class into the child theme, make my tweaks, and add a few lines to my functions.php
file.
The functions.php
changes were incredibly straightforward. I added the following lines:
// Include Recent Posts Excluding Category widget
include("class-wp-widget-recent-posts-exclude-cat.php");
add_action( 'widgets_init', function(){
register_widget( 'WP_Widget_Recent_Posts_Exclude_Cat' );
});
… where class-wp-widget-recent-posts-exclude-cat.php
is the class of my new widget.
You can see the custom version of the widget on Github. The essence of the changes I made was to add one more filter to the code that retrieves the list of posts:
$r = new WP_Query( apply_filters( 'widget_posts_args', array(
'posts_per_page' => $number,
'no_found_rows' => true,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'cat' => $exclude_cat
) ) );
The last condition, 'cat' => $exclude_cat
, is the one I added. If you pass a negative value to the cat
filter, that becomes a category exclusion. In my case, I want to exclude the Micro category, which has an ID of 104
, so $exclude_cat = -104
.