Skip to content

Instantly share code, notes, and snippets.

@albertski
Last active March 6, 2018 22:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save albertski/6af957aba26c5419ae48 to your computer and use it in GitHub Desktop.
Save albertski/6af957aba26c5419ae48 to your computer and use it in GitHub Desktop.
Get Embed View With Title
<?php
/**
* Embed a view with a title. Pretty much copying the views_embed_view() but
* adding the ability to display title.
*
* @param string $name The name of the view to embed.
* @param string $display_id The display id to embed. If unsure, use 'default',
* as it will always be valid. But things like 'page'
* or 'block' should work here.
* @param string $title_tag The html header tag (h1, h2, h3, etc..).
*
* @return string $output Html output.
*/
function myfunction_views_embed_view_with_title($name, $display_id = 'default', $title_tag = 'h2') {
$args = func_get_args();
array_shift($args); // remove $name
if (count($args)) {
array_shift($args); // remove $display_id
array_shift($args); // remove $title_tag
}
$view = views_get_view($name);
if (!$view || !$view->access($display_id)) {
return;
}
$title = $view->display[$display_id]->display_options['title'];
$output = '<' . $title_tag . '>' . $title . '</' . $title_tag . '>';
$output .= $view->preview($display_id, $args);
// Only return output if there is a result.
if ($view->result) {
return $output;
}
}
@benrobertsonio
Copy link

Thanks for this, just what I needed :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment