0

I have a loop which displays the tags, I'd like to add an anchor link to those tags. My code is as follows:

<?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 Answers 2

1

Try this

$brands = get_the_tags(); 
$links  = array();
foreach($brands as $_brand){
    $links[] = '<a href="'.$_brand->url.'">'.$_brand->name.'</a>';
}
echo join(', ', $links);
Sign up to request clarification or add additional context in comments.

3 Comments

Really $_brand and not only $brand in foreach, because you use $brand in loop body?
You can use it $_brand OR $_brand, its only refernce
But then you must use $_brand in loop body too.
1

I assume you want to add link to brand name? Here is the code for that:

<?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 '<a href="add_link_here">'.$brand->name.' </a> ';

                        $count += 1;    
                    }   else {
                        echo '<a href="add_link_here">'.$brand->name.' </a> ';
                    }                           

                } ?></p>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.