在foreach循环中添加链接

时间:2019-04-06 09:04:59

标签: php wordpress

我有一个显示标签的循环,我想向这些标签添加锚链接。我的代码如下:

<?php $brands = get_the_tags(); ?>
<p class="brand-tags">
     <?php 
         $count = 0;
            foreach ($brands as $brand) {
                // echo sizeof($brands);
                if ($count < sizeof($brands)-1) {
                    echo $brand->name.', ';
                    $count += 1;    
                 }  
                else {
                     echo $brand->name;
                 }                          
            } 
     ?>
</p>

2 个答案:

答案 0 :(得分:1)

尝试

$brands = get_the_tags(); 
$links  = array();
foreach($brands as $_brand){
    $links[] = '<a href="'.$_brand->url.'">'.$_brand->name.'</a>';
}
echo join(', ', $links);

答案 1 :(得分:1)

我假设您想添加指向品牌名称的链接?这是该代码:

<script src="/js/main.js?ver=2"></script>