Programmatically render views

Public

The function views_embed_view basically takes two argument in which 1st is view name and is required, 2nd is display ID to render which is optional. You can also pass the 3rd argument to provide the argument to your views.

</> CopyGet raw version
php
  1. <?php
  2. print views_embed_view('VIEWS_MACHINE_NAME', 'DISPLAY_ID', $view_arg);
  3. ?>

Note: The above function views_embed_view() will not print the title of the view. To print the title you will need to do what this function does and print the title separately. Here is sample code:

</> CopyGet raw version
php
  1. <?php
  2. $view = views_get_view('VIEWS_MACHINE_NAME');
  3. $view->set_display('DISPLAY_ID');
  4. print $view->get_title();
  5. print $view->preview('DISPLAY_ID');
  6. ?>