如何在PHP中链接?

时间:2018-06-15 08:56:32

标签: php arrays foreach hyperlink

我想将输出的六个图像中的每一个链接到不同的外部链接,我该怎么做?

<? $accreditations = array(
     'One' => 'picone',
     'Two' => 'pictwo', 
     'Three' => 'picthree',
     'Four' => 'picfour',
     'Five' => 'picfive',
     'Six' => 'picsix'); ?>
<ul class="accreditations<?=$accreditations_extra_class?>"><?

foreach($accreditations as $alt => $img){
    ?><li><picture alt="<?=$alt?>" class="responsive-image">
        <source srcset="<?=get_bloginfo('template_url')?>/inc/img/accreditation--<?=$img?>--74.png, <?=get_bloginfo('template_url')?>/inc/img/accreditation--<?=$img?>.png 2x" media="(max-width: 339px)">
        <source srcset="<?=get_bloginfo('template_url')?>/inc/img/accreditation--<?=$img?>--103.png, <?=get_bloginfo('template_url')?>/inc/img/accreditation--<?=$img?>.png 2x" media="(min-width: 340px) and (max-width: 459px)">
        <source srcset="<?=get_bloginfo('template_url')?>/inc/img/accreditation--<?=$img?>.png" media="(min-width:340px)">
        <img srcset="<?=get_bloginfo('template_url')?>/inc/img/accreditation--<?=$img?>--74.png" alt="<?=$alt?>" />
    </picture></li><?
} // SPACING REMOVED TO GUARANTEE NO EXTRA SPACING ADDED BETWEEN ITEMS ?>

</ul>

1 个答案:

答案 0 :(得分:0)

只需更改$ accreditations变量,使每个数组值指向同时包含图片网址和链接网址的值

$accreditations = array('one' => ['link' => 'http://etc.com', 'img' => 'http://image.png'], 'two' => ...

然后在你的foreach($accreditations as $alt => $img){中,$ img值将等于该数组保持链接和img。因此,要获取图片网址,只需使用$ img ['img']并获取应放入标记的链接,使用包含图片或其他任何内容链接的a href="<?=$img['link']?>">Click here</a>之类的内容