Don’t Worry, You’re In The Right Place
A lot of these posts were originally on SureFireWebServices.com. So if you found them through search engines or clicking around, this is their new home :). Enjoy!
0

How to display the parent term from a custom taxonomy on a post that belongs to multiple terms.

Wow, that’s a long title! I had to do it though. I went crazy trying to find out how to do this and typed so many different things into google, only to find scattered information and crazy code everywhere.

In actuality the solution is quite simple.

Here’s some back story first so that it’s easier to understand exactly what I needed.

The Problem

Laymans: I’m working on a website that will sell websites. Each website belongs to multiple categories. When someone clicks on a category, I want the website to only show the category that was clicked. So if you clicked business, the website would say it’s in the business category and so on.

WordPress Jive: I’m working on a website that will sell themes. Each theme belongs to multiple terms in a custom taxonomy. When someone clicks on a specific term, I want the theme to only display the term of the parent on the archive page. So if someone clicks Business, all the themes associated with the term Business will show up but will only display that they are part of the Business term, even if they are associated with other terms.

Got it. Good. Moving on…

The Solution

Let me first say, I’m not a professional programmer. I’m a front end developer. I know CSS, HTML, some jQuery and a little bit of PHP. When it comes to getting deep into code with functions and arrays and what a function is returning, etc., it does not come easy.

In conjunction with that, wordpress has so many hooks and functions and filters, I have no idea which one to use! Check out the codex for get_terms. Now look at how many functions are related!!

Yeah, that’s what I went through.

I don’t remember where I found this solution but it was a mixture of different code that I eventually pieced together. Check it out:

This code will return the name of the parent term of the post that you’re in!

So how do you actually use this?

Well, I used this to replace the default Genesis Loop so that whenever someone clicked on the term link, it would display each post associated with that term. Each post would then display the term.

The code looked like this (I used the Agent Press theme to test it out):

Now, instead of showing a list of terms that the post is part of, it will only show the parent term of the page that it’s on!

If you know an easier way, or even just a different way, I’d love to hear it!

Advertisement
About the Author

What’s UP! This is my site, I write 99% of the articles on here. I’m also the owner of SureFireWebServices.com. I help out a lot of developers and designers getting into the web game. Helping is fun for me, so feel free to ask me any questions! I’ve made courses and have a membership as well to help get you on your feet!

0 comments on How to display the parent term from a custom taxonomy on a post that belongs to multiple terms.

  1. Carrie Dils says:

    Ah, okay. You’re basically finding a way to customize the page without creating an actual template for taxonomy-theme_category.php). Using your example, I’d like to create a page that just listed the theme categories (or all terms for a particular archive).

    I kept thinking creating a taxonomy.php template (where “taxonomy” is replaced with your taxonomy slug) would achieve this, but I don’t think WordPress automatically creates that archive.

    SO, now I’m thinking about using a custom page template that loops through all the terms for a particular taxonomy and just assigning a page to that template.

    I’ve been living in taxonomy land for few days and it a slippery world here. 🙂

  2. Hussain says:

    Hello,
    I tried this code, It shows current taxonomy name not parent.
    Thanks

    1. Jonathan says:

      You sir, are correct. My apologies, I mis-worded the title. The script actually returns the taxonomy that’s clicked on when viewing a page, which in essence would be the current, not the parent. Sorry about that.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top