• Resolved OrignalCeiling

    (@abandonedar)


    Hi everyone, this topic might seem easy and probably has a simple answer, but for the life of me I can’t get it to work. I used to use to use if ( in_category functions to display different location data. Recently, we have had so many categories, I thought it would be easy to replace them with custom taxonomies to be more organized.

    I think I have tried everything I came across and I’m not sure what I’m doing wrong. I’m using the plugin “CPT UI” to create the taxonamies, but helpful links like:
    https://developer.www.ads-software.com/reference/functions/is_object_in_term/
    https://developer.www.ads-software.com/reference/functions/has_term/
    https://developer.www.ads-software.com/reference/functions/is_tax/
    and many others are not working.

    This is what the code looked like before

    <?php if ( in_category( 'Church' )) { ?>
    &bull; <a href="https://abandonedar.com/class/church/">School</a>
    <?php } ?>

    or

    <?php if ( in_category( 'hotel-motel' )) { ?>
    &bull; <a href="https://abandonedar.com/class/church/">Hotel/Motel</a>
    <?php } ?>

    And these are a few I tried:

    <?php if ( is_tax( 'class', $church_class ); { ?>
    &bull; <a href="https://abandonedar.com/type/church/">Church</a>
    <?php } ?>
    <?php has_term( $hotelmotel = '', $class = '', $post = null ); { ?>
    &bull; <a href="https://abandonedar.com/type/church/">Hotel/Motel</a>
    <?php } ?>
    <?php has_term( $hotelmotel = '', $class = '', $post = null ); { ?>
    &bull; <a href="https://abandonedar.com/type/church/">Hotel/Motel</a>
    <?php } ?>

    So the main issue is that it is only half working. Most of these function will print all functions in “Class” instead of just the ones selected. Any clues as to what I’m doing wrong?

    Thanks in advance!

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • The in_category function calls has_category, which calls has_term, so it seems like the easiest way is to use has_term.
    Your code doesn’t pass any good parameters though.

    <?php has_term('hotel-motel', 'hotel-tax'); { ?>
    &bull; <a href="https://abandonedar.com/type/church/">Hotel/Motel</a>
    <?php } ?>

    I put ‘hotel-tax’ since you didn’t say what the name of the taxonomy is. Replace that with the right name.

    Thread Starter OrignalCeiling

    (@abandonedar)

    Hi! Thank you for your reply. I tried this out, but for some reason it is still wanting to print both Functions instead of just the one selected.

    So I have under a post “hotelmotel” checked, but not “church_class”

    with the code in my theme

    <?php has_term('hotelmotel', 'class'); { ?>
    &bull; <a href="https://abandonedar.com/type/church/">Hotel/Motel</a>
    <?php } ?>
    
    <?php has_term('church_class', 'class'); { ?>
    &bull; <a href="https://abandonedar.com/type/church/">Church</a>
    <?php } ?>

    and it displays: ? Hotel/Motel ? Church

    “Class” is the Custom taxonomy

    Any thoughts?

    Do you have the actual if in your code? (it isn’t here in this example)

    Wouldn’t it be easier to use
    <?php the_terms(); ?>

    Thread Starter OrignalCeiling

    (@abandonedar)

    I just added “if” to the beggining at it is still not working.

    <?php if (has_term('hotelmotel', 'class')); { ?>
    &bull; <a href="https://abandonedar.com/type/church/">Hotel/Motel</a>
    <?php } ?>
    
    <?php if (has_term('church_class', 'class')); { ?>
    &bull; <a href="https://abandonedar.com/type/church/">Church</a>
    <?php } ?>

    How would I use <?php the_terms(); ?> if my context?
    Thank you in adavance!

    Thread Starter OrignalCeiling

    (@abandonedar)

    I just tried this too

    <?php if (has_term('hotelmotel')); { ?>
    &bull; <a href="https://abandonedar.com/type/church/">Hotel/Motel</a>
    <?php } ?>
    
    <?php if (has_term('church_class')); { ?>
    &bull; <a href="https://abandonedar.com/type/church/">Church</a>
    <?php } ?>

    Taking away the taxonomy, but it still shows both.
    Thanks again for your help, I feel like it’s almost there!

    Your code
    <?php if (has_term('hotelmotel', 'class')); { ?>
    has a semicolon before the brace. I think this ends the if statement.

    How would I use <?php the_terms(); ?> if my context?

    That function prints the links to the terms that the post has, so that’s all you need. Try it to see.

    Taking away the taxonomy, but it still shows both.

    Don’t take away the taxonomy because it defaults to ‘category’.

    Thread Starter OrignalCeiling

    (@abandonedar)

    Ok, I think we are onto something with the semicolon. Where is it supposed to go? Because with it there, it shows all terms, but when I delete it, it shows no terms even if one is selected. Hopefully that makes sense.

    <?php if ( is_tax( 'Jail', 'class' )); { ?>
    &bull; <a href="https://abandonedar.com/type/jail/">Jail</a>
    <?php } ?>
    <?php if ( is_tax( 'hotelmotel', 'class' )); { ?>
    &bull; <a href="https://abandonedar.com/type/jail/">Hotel/Motel</a>
    <?php } ?>

    = ? Jail ? Hotel/Motel (Only “hotelmotel” is checked)

    <?php if ( is_tax( 'Jail', 'class' )) { ?>
    &bull; <a href="https://abandonedar.com/type/jail/">Jail</a>
    <?php } ?>
    <?php if ( is_tax( 'hotelmotel', 'class' )) { ?>
    &bull; <a href="https://abandonedar.com/type/jail/">Hotel/Motel</a>
    <?php } ?>

    = ________

    Thank you for your continued help.

    You changed it to use is_tax, which checks if the query was for a taxonomy archive page. And its parameters are opposite of has_term.

    Thread Starter OrignalCeiling

    (@abandonedar)

    YOU DID IT!!!! THANK YOU!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘If in_category function for Custom Taxonomies’ is closed to new replies.