Adding count also to parent categories
-
why this function is working
function custom_add_count_on_archive_title( $title ) { $term = get_queried_object(); if( $term instanceof WP_Term && 'category' === $term->taxonomy ) { $title .= ' <span>'.$term->count.'</span>'; } return $title; } add_filter( 'get_the_archive_title', 'custom_add_count_on_archive_title', 10, 1 );and this other, derivated from the above one, not
function custom_add_count_on_archive_title( $title ) { $term = get_queried_object(); $term_id = get_queried_object()->term_id; if( $term instanceof WP_Term && 'category' === $term->taxonomy ) { $children_terms = get_terms(array( 'taxonomy' => 'category', 'child_of' => $term_id, )); $total_count = 0; if (!empty($children_terms) && !is_wp_error($children_terms)) { foreach ($children_terms as $child_term) { $total_count += $child_term->count; } } $title .= ' <span>'.$total_count.'</span>'; } return $title; } add_filter( 'get_the_archive_title', 'custom_add_count_on_archive_title', 10, 1 );The page I need help with: [log in to see the link]
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
The topic ‘Adding count also to parent categories’ is closed to new replies.