How do I programmatically show a view block?

Solutions

If you want to pass a parameter to the view use:

echo views_embed_view('view_machine_name', 'block_1', $param);

For example:

echo views_embed_view('view_machine_name', 'block_1', $node->nid);

or:

echo views_embed_view('view_machine_name', 'block_1', $nid, $uid);

Use views_embed_view():

echo views_embed_view('view_machine_name', 'block_1');

You can find out the display name by clicking the tab and check its name in the address bar of your browser: #views-tab-block_1 gives you block_1.

tostinni's answer is the correct one in most cases. However, if you want to include the View title, it is a bit more ugly.

From the views_embed_view() documentation:

Note that this function does NOT display the title of the view. If you want to do that, you will need to do what this function does manually, by loading the view, getting the preview and then getting $view->get_title().

For example:

$name = 'view_machine_name';
$display_id = 'block_1';
$view = views_get_view($name);
if (!$view || !$view->access($display_id)) {
  return;
}
$view_content = $view->preview($display_id);
$title = $view->get_title();

Updated: @rojosnow noted that the docs point out you must get the preview before calling get_title() so I've flipped those two lines around.

Tags: Drupal 7 / Views / Blocks

Similar questions

Drupal calling a block programmatically, block title not displaying
I tried calling a block programmatically but it's not displaying the block title. I added a block from the "add block" page. It has an id of 53. I have this code in my page.tpl.php or node.tpl.php: It does show the contents (body) of the block, but not the block title (subject). Any ideas why?
how to hide view block when other view block not giving any search result?
I have created a view(ABC) in which search criteria: "country type content" and other filter is "promoted to front page"."Promoted to front page" filtering content using cck boolen field(yes/no). Another view(XYZ) has search criteria: "country type content" only. When promoted to front page cck field is empty or null my first view(ABC) will hide sa...
After I overrode a block template for a primary links, block-edit-link doesn't show anymore...?
I just created block-menu-primary-links.tpl.php file in the root of my theme, in order to strip off some default structures I have for other blocks, but now that I did that, block-edit-link which was displaying itself by default on hover, doesn't appear anymore. It's not that I want it to back, but I just wonder why did it go away and how can I con...
Block does not show up in block list
recentpost.file recentpost.module I've created a sample module from scratch and can activate it in admin->module, but when I go to structure->block, the block I've just created doesn't show in the list no matter how many times I've cleared the cache both from drupal and the IE. Can anyone help me sort it out.
How can i show only content type fields in block or how can i call a add content type page in a block
How can i call a add node "content type" in a block. I have upload an image below Image1. I have a content type : Upload Resume ie upload_resume Anomynous users can also only create this content type. I want this fields in a block where user can upload resume not in page style bcos of below image shows other factors ie Image2
If one block has content do not show other block
I have a region in my page.tpl.php file called "seminars", and it has two blocks (views) assigned to it, one is called "view_upcoming_seminar" and the other is called "testimonials". The content shows up great for both blocks (views) in the region. What I'd like to do is only show "testimonials" if there is no content in "view_upcoming_seminar". I'...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.