Views can be used to display content as a page, block, etc. You can directly access URL to show page view content, you can add views block in any region if it is to be shown on sidebar first, sidebar second, content top, content bottom. What if the content of views block needs to be shown inside the content of page. So here is sample code to render the views programmatically in Drupal 7 in just one line:
<?php
print views_embed_view('VIEWS_MACHINE_NAME', 'DISPLAY_ID', $view_arg);
?>
The function views_embed_view basically takes two argument in which 1st is required and 2nd is option. You can also pass the 3rd argument to provide the argument to your views. Below are the arguments you can pass to this function:
print views_embed_view('VIEWS_MACHINE_NAME', 'DISPLAY_ID', $view_arg);
?>
- VIEWS_MACHINE_NAME: The machine name of the view you wanted to render.
- DISPLAY_ID: The display you want to render of that view.
- $view_arg: Argument value your view accepts. e.g. I've a view which display nodes created by current logged in user, so this will be $user->uid.
<?php
$view = views_get_view('VIEWS_MACHINE_NAME');
$view->set_display('DISPLAY_ID');
print $view->get_title();
print $view->preview('DISPLAY_ID');
?>
$view = views_get_view('VIEWS_MACHINE_NAME');
$view->set_display('DISPLAY_ID');
print $view->get_title();
print $view->preview('DISPLAY_ID');
?>