I'm using the Widget Logic plugin to dynamically display a recent posts widget.

This is the code I'm using:

$thiscat = get_category( get_query_var( 'cat' ) );
$postcount = $thiscat->count; 
is_single() || is_category() && $postcount > 0;

The problem is, if I'm viewing a single post and that post is the only one in its category, the widget is empty with just the title showing.

I need to change the code so it only displays the widget if there's more than one post in the category.

  • Could you paste your widget's code into your question?
    – Dan.
    Nov 30, 2016 at 9:17
  • @Dan. I've posted an answer to this question already.
    – jrcollins
    Nov 30, 2016 at 10:41

1 Answer 1


I was finally able to get this to work using the following code:

global $wp_query;
$postcat = get_the_category( $post->ID );
$countposts = $postcat[0]->count;
$thiscat = get_category( get_query_var( 'cat' ) );
$postcount = $thiscat->count; 
is_single() && $countposts > 1 || is_category() && $postcount > 0;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.