0

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; 
return
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.

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

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; 
return
is_single() && $countposts > 1 || is_category() && $postcount > 0;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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