admin管理员组

文章数量:1134247

In this moment I have a code to show the count on categories, the problem is that the parent categories display always 0, insted of the sum of posts in children categories

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 );

What I should add to this code?

In this moment I have a code to show the count on categories, the problem is that the parent categories display always 0, insted of the sum of posts in children categories

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 );

What I should add to this code?

Share Improve this question edited Aug 10, 2023 at 7:14 YourManDan 4342 silver badges12 bronze badges asked Aug 4, 2023 at 8:04 Andrea SacconiAndrea Sacconi 797 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

You could add this to your code:

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 );

Have not tested it but theoretically it should work. What it does is gets the children of the current term. Iterates through them and totals the count of them.

This is the right code:

function custom_add_count_on_archive_title( $title ) {
$term = get_queried_object();
$term_id = $term->term_id;
if( $term instanceof WP_Term && 'category' === $term->taxonomy ) {
    
    $children_terms = get_terms(array(
        'taxonomy' => 'category',
        'child_of' => $term_id,
    ));
    
    $total_count = $term->count;
    
    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 );

本文标签: Adding count also to parent categories