Support » How-To and Troubleshooting » Issue with $term->name when Taxonomy term contains spaces

Issue with $term->name when Taxonomy term contains spaces

  • Hi, hopefully someone can help me with this…

    Basically, I’m creating an archive page for a custom taxonomy (brand). I want to show the custom taxonomy term in the title. The code I’m using to do this is:

    <?php $taxonomy = 'brand';
      $cat_name = get_query_var( $taxonomy );
      $term = get_term_by('name', $cat_name, $taxonomy);
      echo '<h1> All products from ' . $term->name . '</h1>'; ?>

    This works fine on pages where the brand is a single word (eg. Seagate), but when the brand has two words (eg. Western Digital) it doesn’t display the term name.

    Am I missing something? Is there something I need to do to make it work when term names contain spaces?

    Any help is appreciated!


Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi,
    can you check in your databse in the _term_taxonomy table make sure that under “taxonomy” you have “brand”,
    And well get to the right output after this(let’s go step by step).
    I’m using on one of my sites names with spaces and everything works fine.

    Hi. Thanks for taking the time to reply.

    Yes, “brand” is definitely in the _term_taxonomy database table under the taxonomy heading. The term_id’s that reference ‘brand’ are all set out ok in the _terms table as well.

    Ah, I’ve just found a fix. Getting the term by slug rather than by name seems to work ok.

    <?php $taxonomy = 'brand';
      $cat_name = get_query_var( $taxonomy );
      $term = get_term_by('slug', $cat_name, $taxonomy);
      echo '<h1> All products from ' . $term->name . '</h1>'; ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Issue with $term->name when Taxonomy term contains spaces’ is closed to new replies.