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.
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…
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!