Last active
March 6, 2018 22:44
-
-
Save albertski/6af957aba26c5419ae48 to your computer and use it in GitHub Desktop.
Get Embed View With Title
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
Thanks for this, just what I needed :)