要在WordPress评论列表中显示评论文章的父分类和子分类名称,您可以通过编写自定义代码来实现。以下是一种实现方法:
首先,您需要编辑WordPress主题的功能文件(functions.php)。请务必在编辑前备份该文件,以防止意外错误。
找到以下目录:wp-content/themes/your-theme-name/
在functions.php文件中添加以下代码:
function display_comment_category_names($comment_text, $comment) {
// 获取评论所在文章的分类
$post_categories = get_the_category($comment->comment_post_ID);
// 用于存储分类名称的数组
$category_names = array();
foreach ($post_categories as $category) {
if ($category->parent == 0) {
// 父分类
$category_names[] = $category->name;
} else {
// 子分类
$parent_category = get_term($category->parent, 'category');
$category_names[] = $parent_category->name . ' - ' . $category->name;
}
}
// 将分类名称添加到评论文本中
$comment_text .= '<br><span class="comment-categories">' . implode(', ', $category_names) . '</span>';
return $comment_text;
}
add_filter('comment_text', 'display_comment_category_names', 10, 2);