I display a list of news which have a category. I wish to have over my news list , a list of all categories. Is this possible in one single view , and therefore with one single SQL query ?

What I want :

<ul> Cat1; Cat2; Cat3; .....</ul> //1st list

<ul> //2nd list
Title 1 
Cat 1
Body 1

Title 2 
Cat 2
Body 2

Title 3
Cat 3
Body 3

All of that with one display of a view with Views module.

I get to see the first list. But not the list of categories above. I don't want several block and combine them in a template, wich means several query.

My view : enter image description here


You can do it in one view as the content for the first list is contained in the second list.

Your view needs to bring in only the second list. You will then override one of the theme functions for your view

enter image description here

probably one in the list of 'Style Output'.

enter image description here

As you can see from this code, it prints out the HTML list format:

<?php print $wrapper_prefix; ?>
  <?php if (!empty($title)) : ?>
    <h3><?php print $title; ?></h3>
  <?php endif; ?>
  <?php print $list_type_prefix; ?>
    <?php foreach ($rows as $id => $row): ?>
      <li class="<?php print $classes_array[$id]; ?>"><?php print $row; ?></li>
    <?php endforeach; ?>
  <?php print $list_type_suffix; ?>
<?php print $wrapper_suffix; ?>

You will treat this code as printing out the second list, so you have to add code to this to print out the first list. You will have to dig through the variables available in the template to find the Category field for each row and isolate it. If the row has already been fully rendered and you don't have access to the category variable, you can then either parse out the category out of the row, or look into the row style output views templates.

Alternatively, you could overrride one of the views hooks that fires after the sql has been run but before the rendering has taken place (hook_views_post_execute is a likely candidate). You will loop through the result and load all of the categories into a list which you then add as an attachment before ($view->attachment_before) the view.


As far as I know you need two views, one for the first list and another for the second. You can "embed" the first view in the Header of the second view using the Views Field View module.

Views Field View:

This module allows you to embed a view as a field in a view. A new field handler is made available, so this can also be used in area (header/footer/empty) handlers as well as rows.

This view handler can accept arguments from fields of the parent view using tokens and pass them into the child view for each row. Raw or rendered token values can be used, as well as static values.

It's highly recommended to use this module in conjunction with views caching. Views Content cache and Cache Actions are good ways of caching views.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.