Embed View without insert_view module
Last modified: September 22, 2008 - 14:46
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);
?>
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
Review Critical
ClipGlobe - World Travel
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