Number of pages in the category tree Drupal 7 + PHP

In Drupal 7, php code will try to count the number of all nodes attached to the current taxonomy term, including all child terms.

<?php     
    if (arg(0) == 'taxonomy' && arg(1) == 'term') {    
        $term = taxonomy_term_load(arg(2));        
        if($term->vid == 1){        
            $term_node = taxonomy_select_nodes($term->tid, $pager = TRUE, $limit = 100000, $order = array('t.sticky' => 'DESC', 't.created' => 'DESC'));
            $term_node_count = count($term_node);            
            $child_term = taxonomy_get_tree($term->vid, $term->tid);            
            $child_term_node_count = 0;            
            foreach($child_term as $child){            
                $child_nodes = taxonomy_select_nodes($child->tid, $pager = TRUE, $limit = 100000, $order = array('t.sticky' => 'DESC', 't.created' => 'DESC'));                
                $child_node_count = count($child_nodes);                
                $child_term_node_count += $child_node_count;                
            }            
            echo $term_node_count + $child_term_node_count;    
        }    
    }
?>

Brief information on the code - first we check that the taxonomy is now open and check the type of taxonomy, in our example the type we need has vid == 1. After that, we load the current taxonomy and through taxonomy_select_nodes. At the same time, the $limit value, in theory, is set to FALSE and this should remove the node limit of 10, but in my case it didn’t work for some reason and I had to manually set the limit to 100000, so that more than ten ID nodes would be displayed. But if 0 is attached to the parent category, nodes are attached to child categories, and you need to display the full number on the parent page, we get a list of all child taxonomies and also display the number for them. In order to sum up the number inside the rebypass, we use the operator +=

$child_term_node_count += $child_node_count;

And outside of the bypass, we already sum up the number of the category itself with the number of subcategory nodes.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
The comment language code.