WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] taxonomy_exists to display alternate size featured image (3 posts)

  1. iragless
    Member
    Posted 1 year ago #

    Hi

    I have an archive page that displays a custom post types with a specific taxonomy. I'm trying to display a different featured image size if it has another specific sub taxonomy. I'm fairly new to wordpress and php so I could be taking the wrong approach. Basically the code I'm trying to use is

    <?php if (taxonomy_exists('spotlight')) {
             print "TRUE";
          }
          else {
             print 'FALSE';
          }
       ?>

    At the moment it always returns false even on posts with that taxonomy.

    Not sure if it's relevant but it is part of a child theme.

    Appreciate any assistance.
    Ian

  2. bcworkz
    Member
    Posted 1 year ago #

    Hmm, something is not right, more than you know. taxonomy_exists() reflects the presence of a taxonomy in the DB AFAIK, not a particular post. So if 'spotlight' is properly registered, it should always return true, not false. You should investigate this further.

    As for checking if a taxonomy is assigned to a particular post, wp_get_post_terms() will do the job. It is meant for getting the actual terms more than checking for a taxonomy, but it does that as well. I'm not sure if there's a simple template tag similar to taxonomy_exists() that checks a specific post.

    Welcome to WordPress, it can be aggravating, but it is also powerful and malleable. Good fun ahead ;)

  3. iragless
    Member
    Posted 1 year ago #

    Thanks

    I figured that might be the case. Anyway I got it to work with wp_get_post_terms() and then using in_array to check for the required taxonomy. Basically this in case it helps someone else.

    $term_list = wp_get_post_terms($post->ID, $taxonomy, array("fields" => "names"));
          if (in_array('Spotlight', $term_list)) {
               ...do stuff for that taxonomy;
              } else {
               ...do something else;
              }

    Not sure why taxonomy_exists() isn't working? Might have a look later and see if I can work it out.

    Regards
    Ian

Topic Closed

This topic has been closed to new replies.

About this Topic