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:
  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:
  1. VIEWS_MACHINE_NAME: The machine name of the view you wanted to render.
  2. DISPLAY_ID: The display you want to render of that view.
  3. $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.
Note: The above function 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 specifically. Here is sample code:
  $view = views_get_view('VIEWS_MACHINE_NAME');
  print $view->get_title();
  print $view->preview('DISPLAY_ID');
Submitted by ychaugule on