Ready Designs

4 8-bit dragons in a line

Displaying Custom WordPress Taxonomy List Items Without the Links

Tutorials

April 21st, 2011

Or: How to Strip <A> Tags From get_the_term_list

Okay, this one is a bit obscure and not terribly complex, but it took me a minute to solve, so here it is for the rest of the world.

I was working on a WordPress custom taxonomy as a way to store and display useful information about a post. Specifically, I was creating a “Venue” type to store nightclub names which could then be displayed easily throughout the theme’s code, as well as inserted via shortcode.

Creating the Custom Taxonomy

Creating the taxonomy itself wasn’t really that hard, and isn’t the main subject of this post, but for the sake of completeness, I’ll cover that briefly first.

span class="st0">'your-custom-taxonomy-name', 'page''hierarchical''label'=>'Taxo Name', 'query_var''rewrite'

In the above code, I’m registering the taxonomy “your-custom-taxonomy-name” to attach to pages (as opposed to posts). I’m declaring it to be a hierarchical type, like WordPress’ native Categories, with the label “Taxo Name” showing up as the label above it in the admin. The “query_var” tells whether it should apply to custom post types, and “rewrite=>true” allows for pretty URLs such as “mysite.com/your-custom-taxonomy-name/item” instead of the default “mysite.com/?your-custom-taxonomy-name=item”.

This declaration in your functions.php file will register the taxonomy with the database and put a new meta box next to your pages.

For more info on custom taxonomies and how they work, check out custom taxonomies in the WordPress codex.

Outputting the Taxonomies Without Links

Now, I wanted to output the taxonomy info into the theme; i.e., I wanted the theme to display the name of the venue in various places. My only problem was that while the most obvious choice for this was WP function “get_the_term_list()“, it outputs the taxonomy terms as links, wrapped in <a> tags. After some mucking around, I discovered two possible solutions.

Method 1: Stripping Out All HTML

The first works quite well if you don’t need to wrap each tag in any kind of code (like list tags or whatnot). Just use “strip_tags()” like so:

span class="st0">'your-custom-taxonomy-name', '', ', ', ''

This will spit out all your taxonomy terms with commas between them but without any HTML around them whatsoever, no matter what you use for the “get_the_term_list()” parameters because “strip_tags()” will just remove them again. Depending on how you’re planning to display your terms, this could be all you need.

Method 2: Starting Without Any HTML Tags and Adding the Ones You Want

On the other hand, I also had another taxonomy I was creating, called Top Features, for storing features of each venue for easy display. I wanted them to output into an unordered list, which, while supported by “get_the_term_list()”, would be removed by “strip_tags()”.

My solution was to use get_the_terms instead.

span class="st0">'your-custom-taxonomy-name''your-custom-taxonomy-name');

  $output = '<ul>''<li>''</li>';
  }
  $output .= '</ul>'

Breaking this down:

  1. “get_the_terms()” outputs an array if there are any terms of the declared type assigned to the post, or a boolean FALSE if not. The first thing I did was run an “if” statement to make sure any terms have even been added to this post.
  2. Assuming they have been added, I store the taxonomy terms in an array variable, “$taxonomy_ar”.
  3. Now I create a new variable to store my output, and stick the opening HTML list tag in it.
  4. Now I loop through the “$taxonomy_ar” array, temporarily naming each item in it “$taxonomy_term”.
  5. For each “$taxonomy_term” I find, I append information to the “$output” using the PHP “.=” append, and I add list item HTML tags around the term I want to display. Note that since “$taxonomy_ar” is a 3-dimensional array, each “$taxonomy_term” still has to be broken down further to pull out its name using “->name”.
  6. After the foreach loop, I close the HTML list tag and echo the final output.

If all went well, you should get an output of terms wrapped in an HTML list but without <a> tags. Yay!

Just to lock everything down solidly, you may also want to check if the custom taxonomy exists at all by wrapping the above in another if statement:

span class="st0">'your-custom-taxonomy-name')) {
  //all that other good stuff

And there you have it, how to display your custom taxonomy tags or categories without wrapping them in links. Hope you found it useful. :)

40 Responses to “Displaying Custom WordPress Taxonomy List Items Without the Links”

  1. jin410 says:

    I’ve been going crazy trying to figure this out.
    This was exactly the help I needed, worked like a charm.

    Thanks for the tutorial!!

  2. James says:

    Thank you so much Roxanne – its people like you that make the WordPress community so wonderful. I’ve bookmarked this one.

  3. Trisha says:

    this is great and so close to what im trying to do!

    is there a way to echo the slugs instead of the names, say to use as a class???

    so the output would look like:
    li class=”tax-slug” (if one category is selected)
    li class=”slug-one slug-two” (if 2 are selected)

    your first example does exactly what I want to do, but I cant figure out how to convert the name into the slug.

    your second example, ive tried a hundred different ways, and it works with the slug, until I assign two categories to a post, then it does all kind of funky things.

    • Roxanne says:

      Hmmm I’m not sure what to do about that. I hadn’t considered what would happen when putting a post in more than one taxonomy, since my application wouldn’t run into that.

      I can’t think of a solution off the top of my head, but if I do I’ll be sure to post it. Likewise, I hope you’ll write again if you find one!

      Thanks for commenting!

      • Trisha says:

        I did finally get it working. I used the code here. Its so similar to your code it cant even tell you what made the difference, but its finally working after hours and hours of messing with code.

        Thanks so much for your great post!

        • Roxanne says:

          I wonder if it’s because they threw get_term() in the mix. At any rate, I’m glad you found a solution! Thanks for posting it. :)

  4. Worked fantastic! Great clear example! I appreciate you posting!

  5. Sumit says:

    This is a very useful article, but what about if I want the list on post listing page and not on single post page.

  6. Elisabeth says:

    Thank U so much for posting your experience on this topic, this didn’t just solve one problem I have been struggling with, but two! So double thanks for this and thanks for explaining it so well! :)

  7. Iain says:

    Hi, great tut, one question how do i get the id of the taxonomy.

    $taxonomy_term->ID;
    doesnt seem to be working.

    Thanks

  8. Johann says:

    Miss Roxanne… All i can say… is… THANKYOU! Literally pulling my hair out over this one. Very Very good write up! Appreciate the sharing of this information! :)

  9. Eric Rasch says:

    I see you have code to check for if the taxonomy exists, but what if the taxonomy is empty (having trouble finding that code)?

    For example, I’m using a custom taxonomy for displaying guest authors. If a post doesn’t have a guest author, I’d like to display the regular author code instead.

    • Eric Rasch says:

      Ok… this wasn’t so hard! Using the example from your Method #1, if you wrap the ‘echo strip_tags …;’ with ‘if ( ! empty( $terms_as_text ) ) { … }’, that seems to do the trick.

      Thanks for a great post!

  10. Kyle says:

    Just used this to perfection. Thanks!
    Kyle´s last blog post ..Digital Learning and Badges

  11. The first method works perfectly for me. Thank you very much for sharing this tip with us. :-)

  12. probablepossible says:

    Is it possible to alter this code to attach a (designated) outside URL to a taxonomy?

    I want to create a ‘publications’ taxa, to link to amazon lists, and I’m exploring different ways to do that.

  13. Henry says:

    I have a taxonomy that has top-level terms and second-level terms. E.g

    Taxonomy name: Sports
    Top levels terms: Tennis, Football, Athletics, Basketball
    Second level terms (under Tennis): Wimbledon, French Open, US Open, Australian Open

    On single.php, how could I output the terms so that they appear like this:

    Tennis
    indent -> Wimbledon
    indent -> US Open
    indent -> Australian Open
    indent -> French Open

    Football
    indent – > and so on..

    I’d like to output the terms as text as outlined in your tutorial but cannot see how I could indent just the second level terms like i’ve shown above.

  14. Nice method to display custom taxonomies. Thanks for sharing.
    Mani Viswanathan´s last blog post ..How Smart Businesses use YouTube to Increase their Customers

  15. Tristex says:

    Thanks so much, If you don’t mind i’m going to share this.

  16. diana says:

    Major THANK YOU for this!
    diana´s last blog post ..365 Project

  17. Dennis says:

    BRILLIANT…. thank you so much! I just needed to strip the HTML tags and this was a huge time and life saver!!!

  18. Ali says:

    Hey,
    First of all thank you for a great tutorial.
    I’m planning to display a list of all clients (custom taxonomy) but remove hyperlinks for only those that don’t have case studies associated to it – though still display them. I want to show the entire list, but the ones with associated post will have hyperlink tags. How can I archive this? I really appreciate your any help in this matter.
    cheers,
    Ali

  19. JP says:

    This worked perfectly for a project I’m finishing up. Thanks!
    JP´s last blog post ..crackin nuts #gangamstyle #superbowl

  20. Roberto says:

    Thank U!! I have a question : how can i display the parent name on the top-list? I have hierarchical custom taxonomies and i need to display parent and child like this:

    Parent
    -Child
    -Child
    Parent
    -Child
    -Child
    -Child

    Thank u!

  21. Tirumal says:

    Nice code.. See my code with different approach.. (see comment luv)
    Tirumal´s last blog post ..Generate array of custom taxonomies using wp_list_categories in WordPress

  22. Andy says:

    Dear,

    I have 2 tag_ID ( aa & bb ) in pa_colour, and I only want to show aa in new custom page, anybody can help me?

    Many thank you
    Andy´s last blog post ..Hợp tác kinh doanh nước hoa

  23. rudolf says:

    To get the top categories in woocommerce email templates, cart items without the link:

    $terms = get_the_terms( $_product->id , ‘product_cat’);
    if($terms) {
    foreach( $terms as $term ) {

    $term = get_term_by(“id”, $term->parent, “product_cat”);
    if ($term->parent > 0) {
    $term = get_term_by(“id”, $term->parent, “product_cat”);
    }
    $cat_obj = get_term($term->term_id, ‘product_cat’);
    $cat_name = $cat_obj->name;
    }
    }
    echo ‘(‘. $cat_name . ‘)’;

  24. Jose Aquino says:

    Hi Roxanne, I realize your solution work to everyone, I have faith your method is correct, but I can’t get run on my wordpress,
    I have a template theme, and this template have portfolio feature, on the theme-init.php there’s the taxonoy defined like this:

    register_taxonomy(‘portfoliocat’, ‘portfolio’, array(‘hierarchical’ => true, ‘label’ => ‘Portfolio Categories’, ‘singular_name’ => ‘Category’, “rewrite” => true, “query_var” => true));

    I run your 2nd method whit this code

    ID, ‘portfoliocat’)) {
    $taxonomy_ar = get_the_terms($post->ID, ‘portfoliocat’);

    $output = ”;
    foreach ($taxonomy_ar as $taxonomy_term) {
    $output .= ”. $taxonomy_term->name .”;
    }
    $output .= ”;

    echo $output;
    }

    And don’t work for me, could you tell me what am I doing wrong?

  25. Jose Aquino says:

    Before I copy my code wrong, this is the code I’m using and don’t work for me.

    ID, ‘portfoliocat’)) {
    $taxonomy_ar = get_the_terms($post->ID, ‘portfoliocat’);

    $output = ”;
    foreach ($taxonomy_ar as $taxonomy_term) {
    $output .= ”. $taxonomy_term->name .”;
    }
    $output .= ”;

    echo $output;
    }
    ?>

  26. Hi,

    Thanks for a awesome piece of code!
    I want to show the name of the taxonomy, before the actual taxonomy term? Can you help me out with that?

    Regards,
    Mathias

Leave a Reply

CommentLuv badge

» «

Happy Folks

    • I would hire Roxanne for another project anytime I have one and feel confident she would do a tremendous job.

      Rachael Masek, UWF Webspinners

    • Roxanne Rocks! Super quick turnaround, amazing communication and a pleasure to work with. She's the best and I highly recommend her for your website/design needs

      Andrea Morelli, Impetuous Style

    • Roxanne shows her skills in social media everyday by managing numerous accounts and keeping them updated to attract traffic. She's capable of creating an online presence and keeping it within the tone and attitude the company desires.

      William Kammer, BDI

    • When a concern comes up that she knows about, she's usually suggesting solutions before I even come to her to discuss the problem.

      William Kammer, BDI

    • Roxanne is an extremely talented programmer. She is able to take an extremely complex storyboard and turn it into a beautiful website. There is no project too big or too small that she can't handle.

      Rachael Masek, UWF Webspinners

    • [Roxanne] brings along creativity, pro-activeness, enthousiasm, a high level of skill and she will think outside of boxes. She's also a quick learner and a very pleasant person to work with.

      Annelies Draaijer, Raglan Shire

    • Roxanne will grasp the bigger picture of any job and is one of those rare species that combines technical skill, creativity and functionality.

      Annelies Draaijer, Raglan Shire

Ready Designs on LinkedIn

Flying Green Dragon