php Drupal 7:获取分类法节点计数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Drupal 7:获取分类法节点计数相关的知识,希望对你有一定的参考价值。

<?php
/**
 * @param tid
 *   Term ID
 * @param child_count
 *   TRUE - Also count all nodes in child terms (if they exists) - Default
 *   FALSE - Count only nodes related to Term ID
 * @param node_type
 *   Count only nodes of one type (string).
 */
function term_count_nodes($tid, $child_count = TRUE, $node_type = NULL) {
  $tids = array($tid);
  
  if ($child_count) {
    $tids = array_merge($tids, term_get_children_ids($tid));
  }
  
  global $language;
  $langs = array($language->language);
  $langs[] = 'und';
  
  $query = db_select('taxonomy_index', 't');
  $query->condition('tid', $tids, 'IN');
  $query->join('node', 'n', 't.nid = n.nid');
  $query->condition('n.status', 1, '=');
  $query->condition('n.language', $langs, 'IN');
  
  if ($node_type) {
    $query->condition('n.type', $node_type, '=');
  }
  
  $count = $query->countQuery()->execute()->fetchField();
  return  $count;
}

/**
 * Retrieve ids of term children .
 *
 * @param $tid
 *   The term's ID.
 * @param $tids
 *   An array where ids of term children will be added
 */
function term_get_children_ids($tid) {
  $children = taxonomy_get_children($tid);
  $tids=array();

  if (!empty($children)) {
    foreach($children as $child) {
      $tids[] = $child->tid;
      $tids = array_merge($tids, term_get_children_ids($child->tid));
    }
  }
  return $tids;
}

以上是关于php Drupal 7:获取分类法节点计数的主要内容,如果未能解决你的问题,请参考以下文章

PHP Drupal Views 1 - 从摘要列表中删除节点计数

如何在 Drupal 7 中的节点页面上显示分类内容

Drupal 7 视图显示具有相同分类术语的节点

php 从Drupal 8节点的分类术语中加载面包屑

Drupal Views:获取与当前节点具有相同分类的节点

drupal 7 - 按分类术语分组