按类型计算的节点数[关闭]


39

我正在寻找一个片段,使我可以显示特定节点类型的总数,例如“ Pages = 167”或“ Products = 10630”。

我应该使用什么代码来实现?

Answers:


34

这是一个函数,它将返回特定内容类型的节点数:

function YOURTHEME_get_node_count($content_type) {
  $query = 'SELECT COUNT(*) ' .
           'FROM {node} n ' .
           'WHERE n.type = :type';
  return db_query($query, array(
      ':type' => $content_type
  ))->fetchField();
}

要在主题中使用此代码,请将函数添加到您的主题中,template.php然后可以像下面这样调用该函数:

echo 'Pages: ' . YOURTHEME_get_node_count('page');
echo 'Products: ' . YOURTHEME_get_node_count('product');

56

您可以使用“ 视图”模块执行此操作。

  1. 创建一个新视图,删除排序选项,字段和其他默认设置
  2. 为“内容:类型”添加字段
  3. 展开右侧的“高级”部分,并将“使用聚合”设置为“是”
  4. 为“内容:类型”添加另一个字段
  5. 在第二个“内容:类型”字段上,单击“聚合设置”
  6. 将聚合类型设置为“计数”
  7. 第二个“内容:类型”现在应看起来像“ COUNT(内容:类型)”

就是这样!如果需要,请调整一些其他设置,例如字段标签和行样式设置。

这是此类视图的导出,因此您可以轻松地导入并试用:

$view = new view;
$view->name = 'nodecounts';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'Node counts';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Node counts';
$handler->display->display_options['group_by'] = TRUE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['query']['options']['query_comment'] = FALSE;
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'none';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
$handler->display->display_options['row_options']['inline'] = array(
  'type_1' => 'type_1',
  'type' => 'type',
);
$handler->display->display_options['row_options']['separator'] = ': ';
$handler->display->display_options['row_options']['hide_empty'] = 0;
$handler->display->display_options['row_options']['default_field_elements'] = 1;
/* Field: Content: Type */
$handler->display->display_options['fields']['type_1']['id'] = 'type_1';
$handler->display->display_options['fields']['type_1']['table'] = 'node';
$handler->display->display_options['fields']['type_1']['field'] = 'type';
$handler->display->display_options['fields']['type_1']['label'] = '';
$handler->display->display_options['fields']['type_1']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['type_1']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['type_1']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['type_1']['alter']['external'] = 0;
$handler->display->display_options['fields']['type_1']['alter']['replace_spaces'] = 0;
$handler->display->display_options['fields']['type_1']['alter']['trim_whitespace'] = 0;
$handler->display->display_options['fields']['type_1']['alter']['nl2br'] = 0;
$handler->display->display_options['fields']['type_1']['alter']['word_boundary'] = 1;
$handler->display->display_options['fields']['type_1']['alter']['ellipsis'] = 1;
$handler->display->display_options['fields']['type_1']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['type_1']['alter']['trim'] = 0;
$handler->display->display_options['fields']['type_1']['alter']['html'] = 0;
$handler->display->display_options['fields']['type_1']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['type_1']['element_default_classes'] = 1;
$handler->display->display_options['fields']['type_1']['hide_empty'] = 0;
$handler->display->display_options['fields']['type_1']['empty_zero'] = 0;
$handler->display->display_options['fields']['type_1']['hide_alter_empty'] = 1;
$handler->display->display_options['fields']['type_1']['link_to_node'] = 0;
$handler->display->display_options['fields']['type_1']['machine_name'] = 0;
/* Field: COUNT(Content: Type) */
$handler->display->display_options['fields']['type']['id'] = 'type';
$handler->display->display_options['fields']['type']['table'] = 'node';
$handler->display->display_options['fields']['type']['field'] = 'type';
$handler->display->display_options['fields']['type']['group_type'] = 'count';
$handler->display->display_options['fields']['type']['label'] = '';
$handler->display->display_options['fields']['type']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['type']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['type']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['type']['alter']['external'] = 0;
$handler->display->display_options['fields']['type']['alter']['replace_spaces'] = 0;
$handler->display->display_options['fields']['type']['alter']['trim_whitespace'] = 0;
$handler->display->display_options['fields']['type']['alter']['nl2br'] = 0;
$handler->display->display_options['fields']['type']['alter']['word_boundary'] = 1;
$handler->display->display_options['fields']['type']['alter']['ellipsis'] = 1;
$handler->display->display_options['fields']['type']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['type']['alter']['trim'] = 0;
$handler->display->display_options['fields']['type']['alter']['html'] = 0;
$handler->display->display_options['fields']['type']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['type']['element_default_classes'] = 1;
$handler->display->display_options['fields']['type']['hide_empty'] = 0;
$handler->display->display_options['fields']['type']['empty_zero'] = 0;
$handler->display->display_options['fields']['type']['hide_alter_empty'] = 1;
$handler->display->display_options['fields']['type']['separator'] = '';
$handler->display->display_options['fields']['type']['format_plural'] = 0;

/* Display: Block */
$handler = $view->new_display('block', 'Block', 'block');

服务器性能看起来“沉重”。
Fedir RYKHTIK 2011年

7
@Fedir,如果您这样认为,则需要了解有关“视图”模块的更多信息。这只是导出的配置,对象上的设置属性在服务器上一点也不繁重。的确,Views模块作为一个整体将比自定义块使用更多的资源,但是最小的共享服务器无法处理任何事情。有充分的理由在整个站点中使用视图:可维护性,安全性,更快的开发和缓存选项。定制代码也可以,但是不要仅仅因为导出需要81行就丢弃视图。
marcvangend 2011年

3
我同意,“视图”模块在许多情况下可能会非常有用。对于当前任务,我将使用简单查询对对象进行计数,因为它会更轻。我不喜欢在哪里可以更快地进行管理。
Fedir RYKHTIK 2011年

11

首选的编程方式是使用EntityFieldQuery类。了解EntityFieldQuery为什么优于db_query()

这是计算Blog类型的Node的示例。

$query = new EntityFieldQuery();

$query->entityCondition('entity_type', 'node') // grab nodes
->entityCondition('bundle', 'blog') // filter by blog type
->propertyCondition('status', 1) // filter by published
->count(); // count

$result = $query->execute();

看到类似的问题


7

我使用EntityFieldQuery做到了这一点。

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
    /* this is the content type machine name */
    ->entityCondition('bundle', 'product')
    /* You can set extra properties using fieldCondition and properties with propertyCondition */
    ->fieldCondition('field_product_status', 'tid', key(taxonomy_get_term_by_name('New')))
    ;

$result = $query->execute();
if (isset($result['node'])){
    $count_of_new_product_nodes = count($result['node']); 
}

3
不幸的是,EntityFieldQuery将不得不从数据库中获取所有节点,然后计算其中有多少个节点。所以这真的很重。使用上面的视图或SQL答案要轻松得多。
马里奥·阿瓦德

5

使用Drush既简单又快捷。

drush sqlq 'select count(node.nid) as node_count, node_type.type from node inner join node_type on node.type = node_type.type group by node_type.type'

给出的输出类似于:

node_count  type
17  category_2012
20  category_2013
19  category_2014
3   competition
19  entry_2012_breakthrough
89  entry_2012_digitalother
50  entry_2012_directdirect
19  entry_2012_filmsecscn
17  entry_2012_insights
12  entry_2012_outdoor
31  entry_2012_promo
19  entry_2013_breakthrough
100 entry_2013_digitalother
40  entry_2013_directdirect

然后,如果您想按特定类型进行过滤,只需使用grep像这样:

drush sqlq 'select count(node.nid) as node_count, node_type.type from node inner join node_type on node.type = node_type.type group by node_type.type' | grep 2014

3

对于感兴趣的任何人,另一种解决方案是使用SelectQuery类的countQuery方法(通过db_select)。

$count = db_select('node')
  ->condition('type', 'some-type')
  ->countQuery()->execute()->fetchField();

但是,我确实更喜欢timofey发布的EntityFieldQuery解决方案。我仅将此作为一种合理的替代方案。


1
SELECT
  COUNT({node}.nid) AS node_count,
  {node_type}.type
FROM {node}
  INNER JOIN {node_type} ON {node}.type = {node_type}.type
GROUP BY {node_type}.type;

在您的代码中使用此查询



0

作为使用“ 视图”模块的答案的变体,您可以“使用”“ 图表”模块随附的视图。只需安装/启用它,无需其他配置,编码等。开箱即用的示例(此链接中的引号)中包含有关此视图的更多详细信息:

...导航到charts/examples/views您的站点中。然后,您应该看到一个柱形图和一个饼图,其后还以表格形式显示。图表和表显示均包含有关每种可用内容类型的节点总数的数据。

笔记:

  • 另外,除了表格格式,您还可以获得一个图表,用于按内容类型可视化节点数。
  • 如果您喜欢该视图,并且/或者它接近您想要的视图,则也可以克隆该视图,然后再次禁用“图表”模块。

披露:我是该模块的维护者,
希望这不会违反网站的自我推广政策

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.