I need to use the image resulting from a view as the background image to a div in my page.tpl.php.

I am able to render the page title with the view below it using:

<div class="container-inner clearfix">
  <h1 id="page-title">
    <span><?php print $title ?></span>            
  <?php $views = views_embed_view('photos', 'block'); print render($views);?>

However, is there a way that I can pull the image from the view, and use it as the background image for container-inner?

  • possible duplicate of How to use entity field as div background image in Views Slideshow? Commented Nov 6, 2013 at 0:00
  • Not sure that these are duplicates. This relates to calling a view from the page template, while the other issue relates to changing what is rendered by a view.
    – Mrweiner
    Commented Nov 6, 2013 at 23:36
  • The image is nowhere else? It's being generated by views? Not coming from any entity?
    – Beebee
    Commented Nov 12, 2013 at 13:20

2 Answers 2


Sure. Just look at what's inside $views and grab the path/url of the image there and stuff that into $background_image and then do something like:

<div class="container-inner clearfix" style="background: url(<?php print $background_image; ?>)">

Sidenote: it might be better to this in a preprocess hook and pass in this as a variable instead of putting this logic in your page.tpl.php file, eg, something like:

function my_module_or_theme_preprocess_page(&$vars) {

  // $views=views_embed_view('photos', 'block');
  $views=views_get_view_result('photo', 'block'); // appears to work easier :)
  $background_image=// however you get the url/path to the image from $views;



which will create a $background_image variable that is now available inside page.tpl.php.

  • So, if I am to use it in a preprocess hook, how can I manually call the correct view? As far as I know, hook_views_pre_render and the like apply to a view that is already being used on the page.
    – Mrweiner
    Commented Nov 6, 2013 at 0:55
  • I meant in the page preprocess hook, eg, don't put all this Views php logic in your page template, but rather just print variables there, and instead do all the Views stuff in your page preprocess function. I added what I mean to the answer above.
    – Jimajamma
    Commented Nov 6, 2013 at 0:57
  • Right, I understand that. I just mean that when utilizing the preprocess hook, how do I call the correct view? When I dpm($view) it utilizes the view currently displayed on the page. But if there is no view on the page (being rendered as a block or a views pages) then there is nothing to print. In my situation, no actual view will be used on the page itself, because all that I need is the image being called by this function. It doesn't need to display anywhere else. Is there a way to force call the correct view within the preprocess hook?
    – Mrweiner
    Commented Nov 6, 2013 at 1:05
  • Ah, yes, Views will put a $view variable in almost every single template it touches. But in your example you were setting a $views variable yourself inside your page.tpl.php and not relying upon $view being there already hence my confusion. But either way, you can call views_embed_view() or views_get_view_result() from just about anywhere you like and use the results equally any way you like.
    – Jimajamma
    Commented Nov 6, 2013 at 1:09
  • Looks like that was the ticket. As a sidenote, though, I had to use views_get_view_result() in order for this to work. When using views_embed_view(), dpm($views) simply returned the raw html for the view as if it were to be embedded somewhere. views_get_view_result() gave me access to the variables. Not sure if you want to should edit your answer accordingly or not. Thanks for the help!
    – Mrweiner
    Commented Nov 6, 2013 at 1:42

Make sure to have a look at Background Images Formatter. It allows you to define a background-image (and size) for any given CSS-selector, as specified in your view (using an image field).

  • I actually just recently used this for an unrelated project. Great suggestion for people still coming across this.
    – Mrweiner
    Commented Nov 5, 2014 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.