Skip to content

Instantly share code, notes, and snippets.

Created July 25, 2014 12:32
Show Gist options
  • Save anonymous/24cada3a79838a7b7267 to your computer and use it in GitHub Desktop.
Save anonymous/24cada3a79838a7b7267 to your computer and use it in GitHub Desktop.
Current Category Recent Posts
//* Recent Posts Widget Filtered by Current Category
add_filter( 'widget_posts_args', 'my_widget_posts_args');
function my_widget_posts_args($args) {
if ( is_category() ) { // Adds the category parameter in the query if we display a category
$cat = get_queried_object();
return array(
'posts_per_page' => 10, // The number of posts shown
'no_found_rows' => true,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'cat' => $cat -> term_id // The current category ID
);
}
else { // Keeps the normal behaviour if we are not in category context
return $args;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment