Community Documentation

Embed View without insert_view module

Last updated September 22, 2008. Created by joachim on February 1, 2006.
Edited by greggles, Michelle, add1sun, Uwe Hermann. Log in to edit this page.

If you're using the Views module and you want to add a view to a certain page where you can't use the insert_view module, you can use the following code to print out your View:

<?php
$view_name
= 'MyViewName'; //name of view
$limit = 3; // number of returns
$view_args = array();
$view = views_get_view($view_name);
print
views_build_view('embed', $view, $view_args, FALSE, $limit);
?>

To display a pager change FALSE to TRUE in the last line.

More details on this in the Views Documentation.

Using Views Default pager and nodes per page settings

In the above snippet the pager setting and nodes per page are hard coded. You may want to use the View settings of Page View or Node View.

To apply Page View settings use this:

<?php
$view_name
= 'MyViewName'; //name of view
$view_args = array();
$view = views_get_view($view_name);
print
views_build_view('embed', $view, $view_args, $view->use_pager, $view->nodes_per_page);
?>

For Node View settings use:
<?php
$view_name
= 'MyViewName'; //name of view
$view_args = array();
$view = views_get_view($view_name);
print
views_build_view('embed', $view, $view_args, FALSE, $view->nodes_per_block);
?>

Comments

This wont work in D6, Views 2

This wont work in D6, Views 2 unfortunately. Anyone know where is this explained for D6?

PS: http://drupal.org/node/246742

****Me and Drupal :)****
Clickbank IPN - Sell online or create a membership site with the largest affiliate network!
Review Critical - One of my sites

In D6

Similar functionality may be achieved in D6 by calling the folllowing:

views_embed_view("view_name","block_1", $arg);

where the 'block_1' is the view variant. For instance, assume I have a view title 'Home_page' with a block variant named 'Home_page_block' and a Page variant named 'Home_page_page'. I could call the block view with:

views_embed_view("Home_page","block_1");

and the Page variant with:

views_embed_view("Home_page","page_1");

Read more here:
http://groups.drupal.org/node/17397

use page_x instead of the display name.

perhaps it is of help to someone. When embedding the view the second parameter should be page_1, page_2 etc. It doesn't work with the name you gave the display.

So if your first display is called "listdisplay", still use page_1 in the embed function.

views_embed_view("view_name","page_1", $arg);

Joel Box - Mondial-IT

thanks

i was getting crazzy with pathauto and views and with your solution i can go on :)

//trying to answer one question for each one that i make.
//this way, drupal will be more friendly and strong

Try this...

Took awhile, but thanks to Merlinofchaos... a solution that works...
http://drupal.org/node/246742#comment-4241898

About this page

Drupal version
Drupal 5.x

Reference

Drupal’s online documentation is © 2000-2012 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.