• 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 } ?>

    <?php if (has_term('hotelmotel', 'class')); { ?>

    <?php if (has_term('hotelmotel', 'class')); { ?>
    &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 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    So, for anyone coming along, in_category() is checking if the current post has a given provided category term assigned to it.

    is_tax() is checking for the taxonomy archive, and doesn’t deal with the current post at all.

    has_term() is probably the one you want most here.

    is “class” your taxonomy slug? or is that just a value you’re trying in this case? Given the provided link, it seems like “type” may also be the taxonomy slug.

    At least from what I can see, “class” isn’t a reserved term, but “type” is, as per https://developer.www.ads-software.com/reference/functions/register_taxonomy/#reserved-terms

    With all that established, i’m going to assume “class” may be the taxonomy slug you chose, and thus has_term('hotelmotel', 'class') should be returning either a true or a false value, assuming hotelmotel is one of your terms associated with this post.

    That said, since I see a link to https://abandonedar.com/type/church/ even though it’s 404’ing, I’m still thinking the taxonomy slug may indeed be “type”.

    Thread Starter OrignalCeiling

    (@abandonedar)

    Thank you for your reply! I figured it out before I saw this and the way it is supposed to look is as follows:

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

    There was a pesky semi-colon that screwed everything up. I appreciate the additional help!

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Aha, yeah, that would do it.

    Glad you got it figured out.

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