Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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:
$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:
$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:
$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
My Drupal 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);
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