隐藏只有一个结果的视图?
我正在使用 Drupal 6 和 Views 构建一系列图像库。一个视图创建全尺寸幻灯片,另一个视图在主图像下方创建缩略图列表。这工作得很好,但是当图库只有一张图像时,缩略图就没有必要了,而且看起来很奇怪。
有没有一种方法可以隐藏只包含一个结果的视图?
I am using Drupal 6 and Views to build a series of image galleries. One View creates a full-sized slideshow, and another View creates a list of thumbnails underneath the main image. This works fine, but when the gallery has only one image, the thumbnail isn't necessary, and looks odd.
Is there a way to hide a View that contains only one result?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,没有任何视图设置可以实现您正在寻找的内容。然而,我想到了两种方法来做到这一点……jQuery 或一些自定义 PHP。
jQuery
视图结果中的每一行都会分配一个
views-row-N
类。因此,您可以使用 jQuery 脚本来查看是否有一个 div(或每行使用的任何其他容器元素)及其上的views-row-2
类。如果没有,那么你只有一个结果。因此,现在您可以使用 jQuery 隐藏具有view-view-name
类的最外层 div(或其他容器元素,但我认为它始终是一个 div)。因此,如果视图被称为“thumbs”,那么它将是view-thumbs
。从技术上讲,该视图仍然存在,但您至少可以对用户隐藏它,这似乎是您真正的目标。自定义 PHP
自定义 PHP 的目标是确定视图中有多少结果,如果不止一个,则将其显示在页面上。有几种方法可以做到这一点,但我认为最简单的方法是覆盖
views-view.tpl.php
主题模板文件。如果您转到编辑视图页面,每个显示都会有一个主题:信息
,就像基本设置
窗格中一样。如果您单击它,它将为您提供该视图的主题模板列表,以及候选名称(Drupal 在对视图结果进行主题化时将查找的文件)。您需要用另一个文件覆盖views-view.tpl.php
文件(不替换,只需覆盖),方法是复制该文件并为其提供以下之一:列表中的名字。该文件将用于显示整个视图结果。如果您打开该文件,顶部会有一条注释,解释哪些变量可用。其中之一是$rows
变量。您可以使用此变量来确定是否有多于一行。如果没有,则不要打印任何内容。如果有,则继续并让默认主题发生。因此,您实际上需要做的就是将默认模板文件中的所有内容包装在 if/else 块中...如果有 2 行或更多行,则执行其中已有的所有操作,否则不执行任何操作。顺便说一句......我总是发现 FireFox 与 FireBug 的组合,Devel 模块 和主题开发模块在处理此类事情时会提供巨大的帮助。
There are no Views settings I'm aware of to accomplish what you're looking for. However, two ways to do this come to mind... jQuery or a little custom PHP.
jQuery
Each row in the view results will have a
views-row-N
class assigned to it. So, you could use a jQuery script to see if there's a div (or whatever other container element is used for each row) with theviews-row-2
class on it. If not, then you only have one result. So now you can use jQuery to hide the outer most div (or other container element, but I think it's always a div) that has theview-view-name
class. So if the view was called "thumbs", it would beview-thumbs
. The view will technically still be there, but you can at least hide it from the user, which seems to be your real goal.Custom PHP
The goal with the custom PHP would be to determine how many results there are in the View, and if it's more than one, display it on the page. There are a couple of ways to do this, but I think the most straightforward would be to override the
views-view.tpl.php
theme template file. If you go to your Edit View page, there's aTheme: Information
like in theBasic Settings
pane for each display. If you click that, it'll give you a list of the theme templates for that View, along with candidate names (files that Drupal will look for when themeing the View results). You'll want to override theviews-view.tpl.php
file with another file (don't replace, just override) by copying the file and giving it one of the names in the list. This file will be used to display the entire view results. If you open the file, there's a comment at the top explaining what variables are available. One of them is the$rows
variable. You can use this variable to determine if there is more than one row. If not, then don't print out anything. If there is, then go ahead and let the default themeing happen. So really all you need to do is wrap everything in the default template file in an if/else block... if there are 2 or more rows, do everything that's already in there, else don't do anything.As a side note... I've always found the combination of FireFox with FireBug, the Devel module and the Theme Developer module to be a TREMENDOUS help when working on stuff like this.
我已经在我的主题中添加了一个自定义的views-view.tpl.php - 我需要计数有几个原因。
我将第一行更改为:
这有助于在您真正需要时进行样式设置。
I've added a custom views-view.tpl.php to my theme - I needed the count for a few reasons.
I changed the first line to read:
This helps with styling when you really need it.
CSS
Unformatted
和HTML list
视图样式使用 CSS 类标记查询的第一个和最后一个结果。如果您可以使用其中之一,那么它就很简单:CSS
The
Unformatted
andHTML list
views styles tag the first and last results of the query with CSS classes. If you can get away with using one of those, then it's as simple as: