I have a view on a blog page in Drupal 7 that I am overriding using a “.tpl.php” or template file so that I can use my own custom HTML and add a title.
Normally, the title of the view can be accessed in the template file using $title or $block->subject but this is not working in my view template file.
Using drupal_get_title() simply displays “Blog” because the page itself is a page template, but I need the title of the block view that is being inserted dynamically with PHP into the page template file.
The weird part is that the title will show in the view preview along with the results of the database query (a.k.a. the fields).
So, how do you access the variable that is normally called $title in the template PHP file?
First, on the view settings page go into “Theme: Information” under the “Other” section. There you find a list of the file names that can be used to override your view output.
So, if your view is called “Test View” then you should create a file called
views-view--test-view.tpl.phpinside youryour_theme/templates/views/folder to override the view’s display settings.In that file, put the following:
Then under “Theme: information” click the button that says “Rescan template files,” and the title should show up now wherever that view is being shown on your site.
If that variable is coming up blank, you can get a list of guesses for what to variable to display by putting the following code in that file:
Each of those variables might be an object or array so you may have to print their content with
print_r($variable)orvar_dump($variable)to get an idea what is inside of them.