I have property as a custom post type. I want to display a specific message if the property is in the sold category. To do that, I’m using the function below:

if ( has_term('sold', 'category')) {
  ?>
  <div class="sold_prop_note">
    <h3>This Property is sold. Take a look at our current exclusives! <a href="https://wordpress.stackexchange.com/properties">View Exclusives</a></h3>
  </div>
  <?
}

Before using the above, I also tried in_category like the one below:

if ( in_category( array( 'sold', 'homepage-sold' ) )) {
  ?>
  <div class="sold_prop_note">
    <h3>This Property is sold. Take a look at our current exclusives! <a href="https://wordpress.stackexchange.com/properties">View Exclusives</a></h3>
  </div>
  <?
}

Both of the haven’t worked. To add the Custom Post Type, the taxonomies were with 'taxonomies' => array( 'category', 'post_tag' ).

Both are being used inside the loop (via a shortcode). Am I missing something?

1 Answer
1

has_term() need third parameter to specify which post type have this term. So, your code would be

if ( has_term('sold', 'category', 'property')) {
  ?>
  <div class="sold_prop_note">
    <h3>This Property is sold. Take a look at our current exclusives! <a href="https://wordpress.stackexchange.com/properties">View Exclusives</a></h3>
  </div>
  <?php
}

Beside that approach, you can use following

global $post; 
if (    ( $post->post_type == 'property' ) 
     && has_term( 'sold', 'category' )
) { ?>
    <div class="sold_prop_note">
        <h3>This Property is sold. Take a look at our current exclusives! <a href="https://wordpress.stackexchange.com/properties">View Exclusives</a></h3>
      </div>
<?php }

Leave a Reply

Your email address will not be published. Required fields are marked *