Community Documentation

Inserting Views (2.x)

Last updated January 19, 2011. Created by Jon Pugh on November 18, 2008.
Edited by aangel, joachim. Log in to edit this page.

There are many ways to insert a view in Views2, but the best way is probably the following function:

From http://groups.drupal.org/node/10129 :
Note that this function passes any additional parameters to the View as Arguments.

<?php
/**
* Embed a view using a PHP snippet.
*
* This function is meant to be called from PHP snippets, should one wish to
* embed a view in a node or something. It's meant to provide the simplest
* solution and doesn't really offer a lot of options, but breaking the function
* apart is pretty easy, and this provides a worthwhile guide to doing so.
*
* @param $name
*   The name of the view to embed.
* @param $display_id
*   The display id to embed. If unsure, use 'default', as it will always be
*   valid. But things like 'page' or 'block' should work here.
* @param ...
*   Any additional parameters will be passed as arguments.
*/
function views_embed_view($name, $display_id = 'default') {
 
//... returns view output, ready to print.
}
?>

Use page_1, page_2, block_1, block_2, ... as $display_id. You'll see the display IDs when hovering over the display names in Vews UI or in the URL when editing the displays.

Example:

<?php
  
print views_embed_view("taxonomy_term", "page_1", 123 );
?>

123 is passed as the argument.
Todo on this page:
  • Best practice for modifying and embedding a view
  • etc

Page status

About this page

Drupal version
Drupal 6.x
Audience
Programmers
Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.