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?
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 the views-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 the view-view-name
class. So if the view was called "thumbs", it would be view-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 a Theme: Information
like in the Basic 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 the views-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.
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:
<div class="<?php print $classes; ?> result-count-<?php print count($view->result) ?>">
This helps with styling when you really need it.
CSS
The Unformatted
and HTML 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:
.view-id-foobar .views-row-first.views-row-last {
display: none;
}
精彩评论