将URL链接添加到图像数组


Add URL Links to an Image array

我正在这个网站poochclub.com上工作,我想添加主页上每个顶部图像幻灯片的链接。

我的代码通过数组调用图像,所以我想知道是否可以向图像幻灯片添加特定的链接,这样如果你点击图像,它们就会进入产品页面?

这是我的代码:

<?php 
                $images = array('welcome.png', 'christmas.png', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg');
                //shuffle($images);
                $i = 0;
                foreach ($images as $im): ?>
                <img src="<?= $theme ?>/images/home/carousel-<?= $im ?>" />
            <?php $i++; endforeach ?>

您也许可以使用关联数组,在这种情况下,您的代码看起来像这样:

<?php 
        $images = array('welcome.png' => 'link1', 'christmas.png'=> 'link2') 
        //shuffle($images);
        $i = 0;
        foreach ($images as $key => $value): ?>
        <a href="<?php $value ?>"><img src="<?= $theme ?>/images/home/carousel-<?= $key ?></a>" />
    <?php $i++; endforeach ?>

我做得很快,所以我不确定是否有任何错误,但这是一般的想法。

像这样:

<?php 
            $images = array('welcome.png', 'christmas.png', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg');
            //shuffle($images);
            $i = 0;
            foreach ($images as $im): ?>
            <a href="<?php $yourLink ?>"><img src="<?= $theme ?>/images/home/carousel-<?= $im ?></a>" />
        <?php $i++; endforeach ?>

$yourLink需要来自您的数据库或其他位置

<?php
$images = array('welcome.png', 'christmas.png', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg');
$links= array('link0.html', 'link1.html', 'link2.html', 'link3.html', 'link4.html', 'link5.html', 'link6.html');
for ($i=0 ; $i<count($images) ; $i++) {?>
  <a href="<?= $links[$i] ?>"><img src="<?= $theme ?>/images/home/carousel-<?= $images[i] ?>" /></a>
<?php } ?>