Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save msankhala/c7f68e5a55ba1c7c55784722d0182998 to your computer and use it in GitHub Desktop.
Save msankhala/c7f68e5a55ba1c7c55784722d0182998 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment