Show/Hide based on Custom Taxonomy Terms

Is it not possible to set conditions based on custom taxonomies (and their terms)? I see “Post Category” and “Post Tag” are included, but no custom taxonomies?

Or am I missing something here?

Hi @owynter,

No, this isn’t currently possible. But Function Return should let you do this for the moment.

Thanks for confirming. Any plans to add it?

Also, what would that function look like?

Hi @owynter
you can try something like this

function Is_current_tax($post, $the_custom_tax, $term_to_find){
$terms = get_the_terms( $post, $the_custom_tax );
if ( ! empty( $terms ) ) {
foreach ($terms as $term) {
if($term->name==$term_to_find) {return true;}
}}
return false;
}

Then you call it in the condition like this:

Show Conditions

  • Function Return

  • Is_current_tax(get_the_ID(),‘the_custom_taxonomy_name’,‘the_term_you_are_looking_to_test’)

  • true

Hope it helps…

J

You PHP ninjas are amazingly weird creatures!

I would be lying if I said I understood all of what you wrote. I probably come off as more knowledgeable that I am :slight_smile: My brain just does not want to fully grasp PHP.

For what it’s worth, my Tax is ‘property_type’ and my Term is ‘villa’

I am certainly not a php Ninja, I code so rarely that I always need to consult the documentation each time I want to build a function ahahaha

I would suggest to use Code Snippet and create a new Snippet with the function I sent you, then you can just use it with your property and villa.

Use the name of your term respecting the case.

Best with it,
let me know if it works, I tested it and it was working on my side.

I added more comments to describe what is happening, it is really simple, I am sure it can be improved, and I am certain you can understand and adapt it if you need. :wink:

Thanks. Going to test it out.

As an aside, try using preformatted text to format your code here. Makes it easier to read. It’s the bracket + slash icon next to the quote icon when you’re writing your post. Your message above would look like this instead:

function Is_current_tax($post, $the_custom_tax, $term_to_find){

// Get_the_terms is a WordPress function that retrieves the terms of the taxonomy that are attached to the post, it return the result as an Array

$terms = get_the_terms( $post, $the_custom_tax );

// Test if the function give any result
if ( ! empty( $terms ) ) {

// Loop through each key/value pair in the resulting array
foreach ($terms as $term) {

// Test if the Name of the Term = The term to find and if so return as true
if($term->name==$term_to_find) {return true;}
}}

// As nothing resulted return False
return false;
}
1 Like

Hmmm. So, still fuzzy on PHP, and I know you’re trying to help, but you probably see/hear code differently than I do.

In your original function, how much of what you typed was actually what should remain in the function, versus values (and variables!) that I need to define?

For example: $terms = get_the_terms( $post, $the_custom_tax );
Should I be defining what $the_custom_tax is?

Sorry, but this is very much a case of “explain like I’m 5 years old”.

You are a php ninja, you are even teaching me the way to display it properly on the forum! Thank you for that!

Regarding the code, nothing to change, all the parameters are passed through variables, I did on purpose so you can use it with ease in any context.

So just place the function as it is in code Snippet,
then call it in Cwicly as I mentionned in the first message.
Change the last 2 parameters with yours, using the names of your CT and term, make sure to respect the case and put them between ’

Let me know if you managed to make it work

(sorry typing from my phone)

best

J

Haha. I wish. I can follow patterns, though.

That said, this isn’t working for me at all. Not sure where I’m messing up. Copied code as shown.

Added the function:

Is_current_tax(get_the_ID(),‘property_type’,‘villa’)

Did I miss something?

are ‘propery-type’ and ‘villa’ the names or slugs?
if slugs you would have to change the php
if not try the names of your ct and terms respecting the exact case

These are the slugs. I’ll try the names. How am i to handle the space in “Property Type”? I think this is breaking my code. Which is why I went with slug in the first place.

And, if I’m going with slug, how do I edit the function?

I would be lying if I said this doesn’t get demoralizing at times.

I don't think spaces would break the code as long as you keep inside simple brackets '    ' 

to change for slug just update this
either copy/paste or inspect the code, 
I just changed $term->name by $term->slug in the last if of the function

[quote="JuGa, post:4, topic:2294"]
function Is_current_tax($post, $the_custom_tax, $term_to_find){
$terms = get_the_terms( $post, $the_custom_tax );
if ( ! empty( $terms ) ) {
foreach ($terms as $term) {
if($term->slug==$term_to_find) {return true;}
}}
return false;
}
[/quote]

Almost there!!!
:crossed_fingers:

Tried this as well. No go.

This actually brings up a separate issue which has been bothering me for a while. There seems to be many little things within a particular installation that can cause Cwicly to break or give unexpected results. I am constantly migrating my builds from one clean installation to another only to find that some things work in one instance and not another.

I can never pin down exactly what might be causing the issue. It’s never the same thing, and never the same feature that doesn’t work as expected. I lose MANY hours every day troubleshooting.

Sorry to read this.

So, you tried both using the name, respecting case and space between simple brackets, and switching the PHP code and calling the function with the slug between simple brackets and nothing works?

:thinking:

And you are using the Code Snippet plugin, saving and activating the snippet and everything…

You are saying it is not working but is there anything unwanted happening?

Correct.

I’ve kinda given up on that. It’s not critical to what I’m doing, so I will just leave it out. But that’s maybe an hour or more of testing that I lost. There are some other, bigger issues that have popped up, and I’m thinking of testing in a new environment again, just to be sure.

I’ve invested a lot of hours into Cwicly to this point, and this week has been one more dark week of little productivity. I just can’t simply sit down and get work done without stopping to double check things not working properly.

Well, I have been there too, but often, I have to say problems were coming most often from my misunderstanding of the software more than the software itself.

I sent you a direct message, offering to have a look at it directly if you need it.

Sorry about being stuck, believe me, I know the feeling pretty well, and I am sure a lot of us here do.
:wink: