Thursday, March 11, 2010

Drupal multiple Views on same page

If you’re using Drupal 6.x and you need to display multiple ‘Views’ on your homepage (or any other page for that matter) here’s the code that will do it for you…

In this example I will be displaying the View called ’showcase’, just change it to whatever name you called your View. The only text you need to change is highlited in red.

You can paste this code into your page.tpl.php file for as many Views as you need!

<?php
    $viewname = 'showcase';
    $display_id = 'showcase'; // or any other display
    $args = array();
    $args [0] = 'foo'; // These values are optional
    $args [1] = 'bar';

    $view = views_get_view($viewname);
    $view->set_display($display_id);
    $view->set_arguments($args);
    print $view->preview();
?>

If you just want to display a View without passing in any variables (args) then you can simply have:

<?php
    $viewname = 'showcase';
    $display_id = 'showcase'; // or any other display
    $view = views_get_view($viewname);
    $view->set_display($display_id);
    print $view->preview();
?>

Boom! Job done.

All the best

Simon

Related posts:

  1. Drupal 6.x Why do I see candidate function names and not candidate template files?
  2. Drupal Clear Top ‘page not found’ errors – How to?
  3. Drupal – The selected file… could not be uploaded. Only JPEG, PNG and GIF images are allowed
  4. IIS and Drupal – Enable Clean URL’s

Leave a Reply