Convert imploded plain text into links

I’m completely open on how to do this:

I have the line:

echo implode(', ',get_field('categories'));

Which is outputting this:

Branding, Web, Print

I have 20 or so options it can output depending on what checkboxes were ticked.

At the moment, what it’s outputting is just plain text. How can I make each tag a link? The link would need to be unique per tag so:

<a href="https://wordpress.stackexchange.com/questions/17563/tags/design/branding">Branding</a>, <a href="tags/design/web">Web</a>, <a href="tags/marketing/print">print</a> 

UPDATE:
The first part of the question is working thanks to Brady. I now have an additional question –

Using Brady’s answer on the line:

$elements[] = '<a href="https://wordpress.stackexchange.com/tags/design/" . strtolower($category) . '" title="' . $category . '">' . $category .'</a>';

If you look at the href it has the link /tags/design/, some of the tags it’s outputting are using the correct link but some are from another section and the link should be /tags/marketing/. How can I account for this? (Not that I know how to do this, someone will need to show me!) Could I create an array with the tags “Branding, Web, Print” and if the $category variable matches this then give it the variable $design. Could I then create another array with “Advertising, Analysis, Campaign” and give it the variable $marketing if the $category matches that. Then in the link I could put href=”https://wordpress.stackexchange.com/tags/$design/$category” or href=”http://wordpress.stackexchange.com/tags/$marketing/$category”?

So a couple of examples would be (I’ll put them as normal links):

From the design section:

<a href="https://wordpress.stackexchange.com/tags/design/branding">Branding</a>
<a href="http://wordpress.stackexchange.com/tags/design/web">Web</a>
<a href="http://wordpress.stackexchange.com/tags/design/print">Print</a>

From the marketing section:

<a href="https://wordpress.stackexchange.com/tags/marketing/advertising">Advertising</a>
<a href="http://wordpress.stackexchange.com/tags/marketing/analysis">Analysis</a>
<a href="http://wordpress.stackexchange.com/tags/marketing/campaign">Campaign</a>

There are only two sections design and marketing, within those sections there are several tags, if that makes sense. The sections in WP are setup like this:

tags > design > (each tag – branding, web, print is it’s own page).
tags > marketing > (each tag – advertising, analysis, campaign is it’s own page).

1 Answer
1

<?php
$categories = get_field('categories');
$elements = array();
foreach($categories as $category) {
    //do something
    $elements[] = '<a href="' . $url . '" title="' . $title . '">' . $name .'</a>';
}
echo implode(',', $elements);
?>

at the //do something here you should find what the URL should be for your category/tag

something like get_tag_link() might be of use.

Also have you looked at using the_tags() It does what your after and can be used in the loop.

Leave a Comment