Random Image
-
Hi Guys, Trying to get this to work:
https://demo.marcofolio.net/php_random_image_rotation/
Not sure what Im doing wrong. Im coding this wrong, so can someone tell me the correct way. Thanks.
Where Im placing it.
<div class=”contentBanner”></div>
My CSS for Div.
}
.contentBanner {
height: 250px;
width: 788px;
float: left;
background: #003 url(images/banner.png);
margin-top: 10px;
margin-bottom: 20px;
}
-
You may add a div to control it, but you need to add info below within the div tags e.g.
<div class="contentBanner"> <img src="images/rotate.php" alt="Rotating Image" width="788" height="250" /> </div>
I believe you want
<div id=”contentBanner”></div>
#contentBanner {
height: 250px;
width: 788px;
float: left;
background: #003 url(images/banner.png);
margin-top: 10px;
margin-bottom: 20px;
}You want id’s not classes
Thanks for the responses.
I tried this:
<div id=”contentBanner”><img src=”rotate.php” alt=”Rotating Image” width=”788″ height=”250″ /></div>
}
#contentBanner {
height: 250px;
width: 788px;
background: url(images/rotate);
float: left;
margin-top: 10px;
margin-bottom: 20px;
}I just have a big blank box that says “Rotating Image”
Thanks for any help
If you just want to rotate header images you’d be better off with a simple function in your theme file…
If you name the images you want to rotate in a numeric sequence it’s only a few lines of code to write…
So let’s say you had images like this… (doesn’t matter where they’re placed – as in the folder)..
image-1.jpg image-2.jpg image-3.jpg
As long as you keep a naming scheme simple you could essentially do that with minimal amounts of code..
Here’s a basic number randomising function..
<?php function randnum() { // Range creates an array from a range (in this case 1 - 10) $b = range(1,10); // So the array looks like - array(1,2,3,4,5,6,7,8,9,10) shuffle($b); $r = array_values($b); echo $r[rand(1,10)]; } ?>
Then you’d place a call to the function wherever, here’s 2 examples..
On an image. <img src="/some/path/to/images/myimage-<?php randnum(); ?>.jpg" alt="" />
Using an element and CSS. <div class="myclass" id="myimage-<?php randnum(); ?>">
With the second option you’d set the background properties using the class, minus the image…
.myclass { background-position:top left; background-color:transparent; background-repeat:no-repeat; } #myimage-1 { background-image:url(images/someimage1.jpg); } #myimage-2 { background-image:url(images/someimage1.jpg); } /* And so on up to 10 (assuming 10 is the limit) */
Just alternate ideas… ??
And my examples are crude, so don’t take them as shining examples!.. lol.. it’s just to give you an idea of how simple a random function can be…. no need for additional scripts…
Thanks for everyone for the help, I know this is not that complicated, but still not working.
As soon as I put this in my functions.php:
<?php function randnum() {
// Range creates an array from a range (in this case 1 – 10)
$b = range(1,10);
// So the array looks like – array(1,2,3,4,5,6,7,8,9,10)
shuffle($b);
$r = array_values($b);
echo $r[rand(1,10)];
}
?>I got this:
Warning: Cannot modify header information – headers already sent by (output started at /home/content/html/wordpress/wp-content/themes/mytheme/functions.php:6) in /home/content/html/wordpress/wp-includes/functions.php on line 784
Warning: Cannot modify header information – headers already sent by (output started at /home/content/html/wordpress/wp-content/themes/mytheme/functions.php:6) in /home/content/html/wordpress/wp-includes/functions.php on line 785
It’s proberly incorrect nesting of the PHP tags..
Revert back to how it was (i assumed you backed it up or reverted the changes)..
And paste just this part..
function randnum() { // Range creates an array from a range (in this case 1 - 10) $b = range(1,10); // So the array looks like - array(1,2,3,4,5,6,7,8,9,10) shuffle($b); $r = array_values($b); echo $r[rand(1,10)]; }
After..
<?php
at the top of the file (the next line is fine).The issue (i think) is the incorrect nesting, which might look like..
<?php some code blah blah blah <?php
or..
?> some code blah blah blah ?>
No opening tags should follow one another, and likewise for closing tags…
So based on the 2 bits above, to fix those, they’d look like this..
<?php some code blah blah blah ?> <?php
and..
?> <?php some code blah blah blah ?>
Hopefully that’s the problem anyway, i did test the code before i posted it.
Thanks for the help.
My functions.php
<?php
if (function_exists(‘register_sidebar’))
register_sidebar();?><?php
function randnum() {
// Range creates an array from a range (in this case 1 – 10)
$b = range(1,10);
// So the array looks like – array(1,2,3,4,5,6,7,8,9,10)
shuffle($b);
$r = array_values($b);
echo $r[rand(1,10)];
}
?>Got this:
Warning: Cannot modify header information – headers already sent by (output started at /home/content/html/wordpress/wp-content/themes/mytheme/functions.php:5) in /home/content/m/a/c/macart/html/wordpress/wp-includes/functions.php on line 784
Warning: Cannot modify header information – headers already sent by (output started at /home/content/html/wordpress/wp-content/themes/mytheme/functions.php:5) in /home/content/html/wordpress/wp-content/themes/mytheme.php on line 785
My header code for the fun of it.
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”https://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
<title><?php if (is_home () ) {
bloginfo(‘name’);
} elseif ( is_category() ) {
single_cat_title(); echo ‘ – ‘ ; bloginfo(‘name’);
} elseif (is_single() ) {
single_post_title();
} elseif (is_page() ) {
bloginfo(‘name’); echo ‘: ‘; single_post_title();
} else {
wp_title(”,true, left);
} ?>
</title><link href=”<?php bloginfo(‘stylesheet_url’); ?>” rel=”stylesheet” type=”text/css” />
<link href=”<?php bloginfo(‘pingback_url’); ?>” rel=”pingback” />
<?php wp_head(); ?>
</head><body class=”twoColFixRtHdr”>
<div id=”container”>
<!– end #header –></div>From this bit of code you posted above..
<?php if (function_exists('register_sidebar')) register_sidebar();?> <?php function randnum() { // Range creates an array from a range (in this case 1 - 10) $b = range(1,10); // So the array looks like - array(1,2,3,4,5,6,7,8,9,10) shuffle($b); $r = array_values($b); echo $r[rand(1,10)]; } ?>
If you remove the bottom most
?>
does that then resolve the problem?I don’t need the header stuff..
You’ve not even placed a call to that function anywhere yet, and the code works here, so i can only assume this being a code placement issue..
Thanks again for the help.
I removed the bottom ?>
Got slammed with same errors.Late one for me, off to bed now, might be easier to simply just provide the complete code from the file.
Use a pastebin though please.
https://wordpress.pastebin.com/Perhaps you’re on an older version of PHP that doesn’t like something in what i’ve done above, hard to say without seeing more of your code though.
I’ll check back after some sleep.. ??
You need to start object buffering in order to do that, thats what the error is. In the header, before any headers are inputted (like sessions or whatnot) you need to put in the code
<? ob_start; ?>
at the very very top of the header (before the <head> even)
and at the bottom of the footer put<? ob_end_flush; ?>
Figured that would help
heasders already sent error is USUALLY the cause of white space at the begining or end of the file.. PHP files do NOT Like any blank spaces before the first <?php tag or after the last ?> tag
So check your file that you were editing, if you can move your cursor before the first or after the last, DELETE until you can not move before the first or after the last
oh yea.. heres a link to a page with common causes of errors. It explains the headers already sent error
CLICK HEREIf you put in the ob_start tag, your errors will go away. Its object buffering and kills most problems, but creates a bit of headway on the server, but nothing notable with newer server technologies.
- The topic ‘Random Image’ is closed to new replies.