Restored guide
Embedding a view in a page or node layout
A View is a query plus a display. Embedding it means rendering that display inside a page, a node, or a region without making the editor paste PHP into the body.
What you are solving
The PHP input filter can call views_embed_view(), and many old tutorials stop there. It also means anyone who can use that format can run PHP. Prefer a custom module, a preprocess function, or a block display placed in a region. Keep the View itself in the Views UI so an editor can still change filters.
Build the display first
Create the View and a block or attachment display. Preview it with the argument you expect, including the case where the argument is missing. Note the view machine name and the display id. Defaults are often default, page_1, or block_1.
Embed from code
function custom_preprocess_node(&$variables) {
$node = $variables['node'];
if ($node->type !== 'article' || $variables['page']) {
return;
}
$variables['related'] = views_embed_view('related_by_term', 'block_1', $node->nid);
}
Print $related from node.tpl.php only after the preprocess function has set it. Passing $node->nid is the contextual argument the display must be configured to receive.
Layout without PHP in the node
If the View is a block, place it in a region and use block visibility on the content type. That needs no template change. Use the template when the markup has to sit inside the node, between the body and the links.
Check before you deploy
- Open a node that should match and one that should not. The empty text must be intentional.
- Confirm the PHP filter is not required for this path.
- Clear the Views cache after you change a display id.
Restored guide for the note archived as “Embedding a view in a page or node layout.”