Posted by mcmahos on August 31, 2005 at 11:40am
I'm a total newbie to drupal, and am slowly but surely finding my way round it, so please forgive me for posting what might be an obvious answer. I've downloaded and successfully installed a rotate script to change the main masthead in the leaf theme. However, what I can't fathom out, being a novice to php, is what file to alter to add in the calling reference for the script.
Thanks
Stuart
Comments
quick answer
You need to edit your page.tpl.php file for the leaf theme (phptemplate version).
if it's of any use..here's a simple random image snippet you could use in an img tag...
<img src="/yoursite.com/rotating-images/blocks-top<?php print (rand(0,5)) ?>.jpg">The snippet points to a random jpg with the following filename in the /rotating-images/ folder.
blocks-top0.jpg
blocks-top1.jpg
blocks-top2.jpg
blocks-top3.jpg
blocks-top4.jpg
blocks-top5.jpg
Dub
What about CSS?
Okay, I REALLY like the Manji theme for my site. A lot. I've made 10 mastheads for the site, and I'm all ready for random rotation of them.
And then I get stuck. I read a few of these posts about randomized mastheads (I love it when sites already have what I'm looking for), and I go into my page.tpl.php file, and I find the word "masthead", and there's no image tag to manipulate. None. Instead it's "id='masthead'".
Thats right, friends, in the Manji theme the masthead is the background image of a div. As far as I can tell I can't insert a php string in a CSS document. (The div comes through blank when I do.)
Any ideas? I really want this to work.
THANK YOU!
- Jon
I'm also wondering how to
I'm also wondering how to make background images rotate...
Thanks - but still stuck
Thanks for the quick reply. But I'm still a bit stuck. As I said I'm a bit of a novice at the moment as far as php is concerned. At the moment I have my rotating script sourced at mysite.com/drupal/themes/leaf/headers/rotate.php. The header images are also in the same directory. All I need is the code for inserting into the page.tpl.php file, and if there's a specific place that it should be inserted. Sorry to be a complete dork but I'm starting to get really confused!
Thanks
Stuart
Got it!
Sorry - I was a bit quick off the mark in the last reply. I've followed it through again and with a bit of trial and error I've got it working. Many thanks once again.
Regards
Stuart
Where did you put it?
Just following the thread, would be intrested in know knowing where you put it in the page.tpl.php filr?
_Guy
Closure
I found this thread because of Jonkun227's comment about no one answering questions on the forum and figured I'd post an answer here for some closure. I had a similar situation where a theme used a <div> with a background property for the banner and I wanted it to rotate. The solution was surprisingly simple. I grabbed the rotation script from http://www.alistapart.com/d/randomizer/rotate.txt and put it in the theme folder as rotate.php. I also created a sub-directory with all my banner images and edited the "folder" setting in rotate.php to use that directory. Then in style.css find the style definition for the banner div and change the background to be rotate.php. In this case it would look something like this:
.masthead {background: url('banners/rotate.php');
}
Now if you want to go the route of editing the page.tpl.php file you can do that too. You need to find the <div class="masthead"> and get rid of the 'class="masthead"' part. Then add the following code (or similar) between the open and closing div tags:
<?php$dir = 'banners'; // Change this to the directory where your banners are
$files = array();
$handle = opendir($dir);
while ( ($file = readdir($handle) ) !== false){
$files[] = $file;
}
if (count($files) > 0 ){
$num = time() % count($files);
$img = $files[$num];
}
print '<img src="' . $img . '" />';
?>
My favorite method involves using the views module. It gives a lot of control through the familiar Drupal UI and very little work to do in the template. Basically, you make a view (called "banner" here) that will generate a random image (you only want the image, so you need to theme the node or view appropriately) Then add the following to page.tpl.php
<?phpprint views_build_view('embed', views_get_view('banner'), FALSE, FALSE, 1);
?>
That's it! Now not only do you have a simple way to get rotating banners, you can leverage the power of Drupal by using Taxonmy, access control, publishing options, etc to manage and display rotating banners. I know this post is old, but at least now it has an answer.
fastforward to 2009
For anyone following along, I've come from the future to say that Views 2 (D6/D7) now uses views_get_view (views API reference) instead of views_build_view.
<?phpprint views_get_view('some_view_name');
?>
Easy-peezy.
Back to this thread: I'm looking for a way to extract only the resultant image url for use in a style="background-image:url(somephp)" on the header div. Somebody please tell me a much cleaner way than this hack job:
<div id="header-inner" class="clear-block" style="background-image:url(/<?php print trim(strip_tags(views_embed_view('header_image_rotate'))); ?>);">which actually does work:
<div id="header-inner" class="clear-block" style="background-image:url(/sites/default/files/imagecache/header-image/Luigis_185.jpg);">--
..happiness is point and click..
http://www.bronius.com
Theming
Use the theme info dialog in Views to determine an appropriate template to use to theme the entire view. Then make the output be only the URL to the image.