PDA

View Full Version : Looping an Image


cocoonfx
11-16-2011, 10:14 AM
Hi Guys

I have 10 images called Banner1.jpg to Banner10.jpg

Existing code:
<a href=""><img src="<?php echo get_template_directory_uri();?>/images/banner1.jpg" alt="" title=""/></a>

I want to dynamic pull the images using PHP. I am trying to use the following:

<?php
$i=0;
do
{
$i++;
echo "<a href="">"."<img src=\"images\banner" .$i . ".jpg></a>"\n";
}
while ($i<=10);
?>

However I am getting a T error and I can not work out what I am doing wrong. Any assistance would be appreciated.

cocoonfx
11-20-2011, 04:47 PM
Can anyone assist?

edbr
11-21-2011, 12:12 AM
directory_uri typo?

and
echo "<a href="">"."<img src=\"images\banner" .$i . ".jpg></a>"\n";
looks wrong.
href "link"
also de bug by echoing individual strings ( such as $1

cocoonfx
11-21-2011, 12:18 AM
Hi Edbr

The directory_uri is actually correct. I am changing an existing wordpress template. Looks odd I know.

edbr
11-21-2011, 12:58 AM
Ah. whats the actual error say? is there a line number?

DWcourse
11-21-2011, 06:03 AM
echo "<a href="">"."<img src=\"images\banner" .$i . ".jpg></a>"\n";

You've escaped some of the quotation marks (the one before images) and not others. And for WordPress you need to use / as the separator not \

cocoonfx
11-21-2011, 01:44 PM
so it should be ?

echo "<a href="">"."<img src=/images/banner .$i . ".jpg></a>"\n";

cocoonfx
11-22-2011, 11:50 AM
Ok I have managed to get the code to work 80% of the way.

Please see below.

<?php
$u= '/dev/images/banner';
$f='.jpg';
$i=0;
do
{
$i++;


echo "<img src=".$u."".$i."".$f.">\n";

}
while ($i<9);
?>

However wordpress uses the following to pull the full director path

<?php echo get_template_directory_uri();?>

i.e www.domainname.co.uk/dev/

However I can not get this part now to work. I have tried:

$p= <?php echo get_template_directory_uri();?>;

I get a T error. Any ideas on how to get this part to work?

DWcourse
11-22-2011, 03:28 PM
if you're setting a variable, don't use echo. Echo writes to the screen.

cocoonfx
11-22-2011, 03:51 PM
Thanks DW and Edbr for directing me in the right direction and making me problem solve this... this works below very well.

<?php
$u= '/images/banner';
$p= get_template_directory_uri();
$f='.jpg';
$i=0;
do
{
$i++;


echo "<img src=".$p."".$u."".$i."".$f.">\n";

}
while ($i<9);
?>