Skip to: Site menu | Main content

Adding Node Counts to View Titles in Views 1.x

It is often desirable to add a node count to the title of a view to help users gauge how many nodes have been returned - especially when using exposed filters.

In Views 1.x, this is a fairly simple process using the "hook_views_pre_view(&$view, &$items)" function (documentation).

Let's say you have a view that displays titles and authors all content of type "story" on your site in table format. Furthermore, you've exposed the "Node: Author Name" field as an exposed filter for the view:

Base view display

As the user plays with the "Author Name" exposed filter, as expected the title of the view doesn't change.

To modify the title, we can implement the hook_views_pre_view function as follows:

function view_title_count_views_pre_view(&$view, &$items) {
if (($view->name == 'test_view')) {
$view->page_title .= ' ('. $view->total_rows .')';
}
}

For this example, I created a new module called "view_title" and a view called "test_view". The "total_rows" value is part of the $view array, so it is as simple as appending the total_rows to the existing page_title. The result is then:

View with title count

While it is possible to affect the view title without writing code when using arguments, I don't believe it is possible to add the node count to the view's title regardless of if you're using arguments or exposed filters.

Feel free to download the "view_title_count" module I wrote for this post below.

As far as doing this same type of thing in Views 2.x, I haven't yet figured it out - it does appear that the "total_rows" variable is not used in Views 2.x, so an alternative way will need to be found. If you know how to do this, feel free to leave it in the comments.

AttachmentSize
view_title_count.zip783 bytes
Submitted by michael on Fri, 11/14/2008 - 3:23pm
Filed under:

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Thanks

Thank you for this. I am very new at this time of implementation. Do i need to post anything in the page.tpl.php file? I have your code enabled but nothing shows up. My view name is: writters_page trying to count content type: news

Do I need to change the $view->page_title part?

writters_page

<?php

function view_title_views_pre_view(&$view, &$items) {
if (($view->name == 'writters_page')) {
$view->page_title .= ' ('. $view->total_rows .')';
}
}

I am using Drupal 5.18

Nope

Nope - you shouldn't have to add anything to the page.tpl.php file. Try clearing Drupal's cache and see if that works.

-mike

Cool, now how do we remove the "Results 1 to x of y"

This is great,
now is there a way to REMOVE the "Results 1 to x of y" from the bottom of a View, using only code within the view definition (i.e. without access to the theming engine)?

Turn off pager?

Colin_e,

I'm not sure exactly what you're asking - it seems like you're talking about some search results, not a view.

If you're sure you're looking at a view, then try just turning off the pager for the particular view (see the "Use pager" checkbox in your view configuration page).

-mike

Views 2 ?

An idea for count the nodes a views in the second version of view ?

Thanks !

@akahn - While I haven't

@akahn -

While I haven't tried it, if you really wanted to do this at the theme layer, I suspect you could do it via a phptemplate_views_view override.

Copy the contents of theme_views_view into your template.php file as phptemplate_views_view, and change the lines


if ($type == 'page') {
drupal_set_title(filter_xss_admin(views_get_title($view, 'page')));
views_set_breadcrumb($view);
}

to


if ($type == 'page') {
drupal_set_title(filter_xss_admin(views_get_title($view, 'page')) . ' (' . count($nodes) . ')');
views_set_breadcrumb($view);
}

Again, totally untested, but I imagine it would work.

Thanks for this idea. I have

Thanks for this idea. I have created a module that adds a checkbox to the views edit form to turn this on and off, and am finding it really useful.

more difficult

In my opinion, it would be more difficult to do in the theme layer than with a small module like I showed. I believe you'd have to do it in your template.php file and modify the $vars['title'] variable in the _phptemplate_variables() function. You'll have to figure out how to get the $view object to access the "total_records" value.

Doing it as a module allows you to leverage the Views API and do it in (probably) less code.

-mike

How could you do this at the

How could you do this at the theme layer to provide a template variable that contains the count?