I want to show all attached terms in my product archive template. I know I can use query loop to query terms and select what I want. But I just want to show them in h1 heading, so I don’t want to use query loop. Any ideas?
function add_category_to_title($title, $id = null) {
// Check if it's a single post view and the post ID is available
if (is_single() && !is_null($id)) {
// Get the categories of the post
$categories = get_the_category($id);
if (!empty($categories)) {
$title .= ' - ' . $categories[0]->name;
}
}
return $title;
}
// Add filter to modify the title
add_filter('the_title', 'add_category_to_title', 10, 2);