Last updated June 9, 2010. Created on June 9, 2010.
Edited by redndahead. Log in to edit this page.

Thank you to aaron.r.carlton for this solution.

Problem:

The slideshow doesn't work when using views_embed_view in tpl.php

Why:

When you use views_embed_view in your tpl.php file the javascript has already been rendered for the page so the necessary javascript for views_slideshow isn't loaded on the page.

Solution:

In your theme's template.php add:

function mythemename_preprocess_page(&$vars, $hook) {
  // Render the view.
  $vars['embedded_view'] = views_embed_view('page_image_rotator', 'block_1');
  //Reload the javascript into the scripts.
  $vars['scripts'] = drupal_get_js();
}

In the tpl.php file where you are embedding the view put:

  <?php print $embedded_view; ?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

onorarono’s picture

I encountered this problem for the first time today but my view is intended to be embedded into my front page before as

<?php
 
print views_embed_view('slideshow','page_1');
?>

Using your solution, my view is gone now and I'm wondering if there's a simple fix I'm overlooking, being unfamiliar with the core files of Drupal (I dunno what you mean by tpl.php).

redndahead’s picture

Are you sure your view is a page view? Is your view named slideshow? In your theme you should have a page.tpl.php. In the case of just your front page you would need a page-front.tpl.php file. Which more than likely would be the same as page.tpl.php, but just with the changes you need for the front page. Look here to learn more: http://drupal.org/node/317461

onorarono’s picture

Excuse my sloppy post from before - my situation is as follows:

My site's front page is a block named Featured Articles which I've made visible on the front page only. Within it I've used the views_embed_view method to display multiple working views by doing the usual

<?php
 views_embed_view
($name,$display);
?>

within the block body. Since your solution doesn't directly match my circumstances, do you know if there's a way to get it to work?

I am guessing that since I'm trying to call $embedded_view from a block I'm by and large stuck.

hermes_costell’s picture

aaron.r.carlton's solution worked perfectly for me.

carvalhar’s picture

Hi,

I cant use my views pager with ajax because i'm embedding the views at page.tpl.

I tried this solution but didn't work.
Also, theres one post here about it:
http://drupal.org/node/928882#comment-3518898

Any help?

jubinri’s picture

I have spoiled 5 hrs to fixing this problem.

This solution is perfect with views_embed_view function + views_slideshow module.

Thanks.

wavesailor’s picture

First you add the views_slideshow block to your content region. Then you create a div container on the page <div id="my-rotator">&nbsp;</div> where you want it to appear (edit the node)

Then in your page.tpl.php add the following script but you'll need to replace block-views-my_image_rotator-block_2 with the name of your views block - find it by using Firebug:

  <script type="text/javascript">
    $(document).ready(  function(){

      var oNewContainer = $("div#my-rotator");
      var oRotator = $("div#block-views-my_image_rotator-block_2");

      oNewContainer.append(oRotator);

    });
  </script> 

PS. You can control on what page the views-slideshow appears by configuring the block

xamount’s picture

I thought I was having this problem as described on this page. But after many hours I found out my problem was related to using views_embed_view with a views jcarousel and the Omega theme (the views jcarousel pagers were not initializing inside of a lightbox). There is a specific patch for this here: #1450846: jCarousel is never initialized properly with Omega theme.

I decided to leave a comment here hoping that it will save someone hours of pulling out their hair.

phponwebsites’s picture

I've one doubt. I've created views, set 1 hour cache and display views using views_embed_view().
But wehn i display page, i got error message "Notice: Undefined index: data in views_plugin_cache->restore_headers() (line 242 of /sites/all/modules/contrib/views/plugins/views_plugin_cache.inc)."

Actually where is the problem? how to solve this?