滚动
Views:当使用多值字段时如何去除节点重复显示
最近我在使用 Views 输出公司列表时,发现 Views 会重复显示 1–3 条相同的记录,这让我很困惑。尝试删除过滤器和排序条件后,问题依然存在 —— 节点仍然重复。原来问题出在节点包含了一个具有多值的日期字段(multi-value field),正是这个字段导致了公司节点的重复。下面是我解决 Views 节点重复 问题的方法,或许也能帮到你:
首先创建一个自定义模块,我将它命名为 sitemade
。文件 sitemade.info
的内容如下:
name = Views Remove Duplicates description = Removes duplicate nodes. Requires editing the module file to identify the views you want to affect. package = "Views" core = 6.x dependencies[] = views
然后创建文件 sitemade.module
,内容如下:
<?php function sitemade_views_pre_render(&$view){ $used_nids = array(); if ($view->name == 'companies'){ if ($view->current_display == 'page_1'){ foreach ($view->result as $row){ if (!in_array($row->nid, $used_nids)){ $new_view_result[] = $row; $used_nids[] = $row->nid; } } $view->result = $new_view_result; } } }
其中 'companies'
是我的 View 名称,而 'page_1'
是该 View 的页面显示(display)名称。添加这段代码后,节点在 Views 中就不会重复显示了。
我在以下英文文章中找到了这个解决方案以及其他类似问题的处理方法: