Is there a way to hide the widget completely if there are no comments yet?

I was thinking about maybe adding a php function to my functions.php file - is that a possible way? If so, can someone provide a suggestion?

  • You'd better make your own extend of Widget Class for this
    – JMau
    Commented Jun 21, 2014 at 13:57
  • I haven't had time to test it yet, but I think Wyck's answer would be the solution. I'll mark the answer as soon as I tested it.
    – okiedokey
    Commented Jul 28, 2014 at 11:47

2 Answers 2


I don't know if there is a hook for the comment widget but you can call a widget in your template with a conditional to check if there are comments. You can probably also do this via functions.php.

Something like:

$num_comments = get_comments_number();

if ( $num_comments > 0 ) {  
    the_widget( 'WP_Widget_Recent_Comments' );
} else { 
 // do something else

Here's one idea using the sidebars_widgets filter from the wp_get_sidebars_widgets() function that's called within the dynamic_sidebar() function:

 * Remove the recent comments widgets from a given sidebar (index)
 * @link http://wordpress.stackexchange.com/q/151361/26350
! is_admin() && add_filter( 'sidebars_widgets', function( $sidebars_widgets ) {

    // ------------------------
    // Edit this to your needs:
    $sidebar_index = 'sidebar-1';
    $find          = 'recent-comments';
    // ------------------------

    if( isset( $sidebars_widgets[$sidebar_index] ) && 0 == get_comments_number() )
        foreach( $sidebars_widgets[$sidebar_index] as $key => $widget )
            if( false !== stripos( $widget, $find ) )
                 unset( $sidebars_widgets[$sidebar_index][$key] );

    return $sidebars_widgets;

where you must remember to modify the $sidebar_index and $find to your needs.

We could also loop over all the sidebars, instead of targetting a single sidebar. I'm sure you can adjust the code snippet if you need that.

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.