2

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="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="/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="/tags/$design/$category" or href="/tags/$marketing/$category"?

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

From the design section:

<a href="/tags/design/branding">Branding</a>
<a href="/tags/design/web">Web</a>
<a href="/tags/design/print">Print</a>

From the marketing section:

<a href="/tags/marketing/advertising">Advertising</a>
<a href="/tags/marketing/analysis">Analysis</a>
<a href="/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).

5
  • can you give us a few examples of what get_field() will return? Maybe do a print_r() on it. As far as I know get_field() is not a built in function.
    – Scott
    Commented May 18, 2011 at 10:58
  • I don't mind if I have to write out all the tags and do some kind of if statement to check if it's within the design or marketing section. I just don't know how to do it.
    – Rob
    Commented May 18, 2011 at 11:34
  • I think you've miss understood me. Where does get_field() come from? are you using a plugin or theme that creates this function? Because its not native WP then its hard to solve this issue without know what this function is doing.
    – Scott
    Commented May 18, 2011 at 12:21
  • Sorry, I'm using this plugins.elliotcondon.com/advanced-custom-fields to create custom fields. The 'get_field()' is needed to grab the checkbox info from WP.
    – Rob
    Commented May 18, 2011 at 12:25
  • I guess it's kinda related to my other question - wordpress.stackexchange.com/q/17560/5415
    – Rob
    Commented May 18, 2011 at 12:32

1 Answer 1

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.

8
  • Thanks, I've adjusted the link line to this $elements[] = '<a href="/tags/design/' . $category . '" title="' . $category . '">' . $category .'</a>';
    – Rob
    Commented May 18, 2011 at 10:33
  • Is it possible to make the $category in the href lowercase? As it is just puts it in as uppercase causing a 404 not found.
    – Rob
    Commented May 18, 2011 at 10:34
  • 1
    use php function strtolower()
    – Scott
    Commented May 18, 2011 at 10:41
  • Yay thanks, I've just noticed something, I'll update my question.
    – Rob
    Commented May 18, 2011 at 10:43
  • @Rob - the last link in your example has a different structure - it is linking to /tags/marketing/ instead of /tags/design/ - is this the case with some of the tags? in which case, you may need to use something like codex.wordpress.org/Function_Reference/get_term_by to get the link.
    – Michael
    Commented May 18, 2011 at 10:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.