How can we embed views into content?


To insert views into node content, we use "views_embed_view" function. This is a simplest solution to inset views into node content. This function does not display the title of the view.

{syntaxhighlighter brush:php;} views_embed_view($name, $display_id = 'default', $arg) {/syntaxhighlighter}

$name : name is the machine name of the view to embed.

$display_id : The display id of the view to embed. A view can have multiple display, so you need to assign correct dispaly id of the view which you want to embed. If you are not sure about the display id of view, then defined "default" as display id. You can also defined "block" or "page" as display id, if there is block or page display exist in your view.

$arg : This is the arguments passed to view. If your view contains the arguments, you can defined the value for this otherwise you can skip this parameters.

Finding the correct Parameters:

  • Go to administration -> structure -> view
  • Edit any view which you want to embed. The url of the view will be as "admin/structure/views/view/machine_name_of_view/edit".
  • Hover on block or page display of view which you want to embed. The url of display defined as "admin/structure/views/view/machine_name_of_view/edit/dislay_id".

Now, you can get the correct parameters of the view for the above function.

Example: embedding "recent comments" block in node content.

  • Go to administration -> structure -> view.
  • By default, "recent comments" view are disabled. Enabled the view by clicking on "enable" link in operations of the view.
  • Edit the "recent comments" view and hover the block display and it will display the "machine_name" and "dispaly_id" of the view.

Now, in the node.tpl, write the below code to embed recend comments in the node content. As there is no arguments in the view, so you can skip the arguments parameter.

{syntaxhighlighter brush:php;} print views_embed_view('comments_recent','block'); {/syntaxhighlighter}



This article is very helpfull for me.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Enter the characters shown in the image.