Since you are in a loop you can use the function get_comments_number()
.
Retrieves the value of the total number of comments, Trackbacks, and Pingbacks for a post. This tag must be within The Loop. Unlike
comments_number()
this function will return the value as a numeric value.
Use it for example as follows:
$num_comments = get_comments_number();
if ( $num_comments > 0 )
$output .= "...";
So your code could look like this:
/* Shortcode to output recent posts from one category */
function display_cat_recent_posts() {
$args = array(
'post_type' => 'post',
'posts_per_page'=> 5,
'cat'=> 10,
);
$cat_recent_posts = new WP_Query( $args );
if ( $cat_recent_posts->have_posts() ):
$output="<ul>";
while ( $cat_recent_posts->have_posts() ) : $cat_recent_posts->the_post();
if ( get_comments_number( ) > 0 ):
$output .= '<li><a href="' . get_permalink() . '" title="' . get_the_title() . '">' . get_the_title() . '</a></li>';
endif;
endwhile;
$output .= '</ul>';
endif;
return $output;
wp_reset_postdata();
}
add_shortcode( 'recent-posts', 'display_cat_recent_posts' );