Show WordPress comments by category only

Recently I needed a widget or plugin which is able to show only the comments from th current category. It seems that this kind of widget is not very common, so I checked the code from several themes I used in the past and also some plugin. The function below should do the trick, place this function into your functions.php file (theme directory) and add the function into your sidebar.

Features included:

  • Shortened comments snippet
  • the comment author’s name
  • filter by one or more category IDs
  • shorten the post title if needed
  • Show the comment date (local date format)

Inside this function another custom function is called, include that function too:

How-to use the current category ID inside that function?

$myCategory = get_the_category();
$catId = array($myCategory[0]->cat_ID);
echo my_recent_comments(10, 100, $catId);

The SQL statement inside the comment function is based on a plugin I found on the WordPress website.