WordPress.org

Ready to get started?Download WordPress

Forums

Issue with $term->name when Taxonomy term contains spaces (4 posts)

  1. jamodu
    Member
    Posted 1 year ago #

    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!

    Thanks
    Dunc

  2. jnhghy
    Member
    Posted 1 year ago #

    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.

  3. jamodu
    Member
    Posted 1 year ago #

    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.

  4. jamodu
    Member
    Posted 1 year ago #

    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>'; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic