4

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="/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="/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?

6
  • Is this page an archive/listing that's been placed inside a page template with a query_posts or WP_Query at the top of the template?
    – Tom J Nowell
    May 21 '16 at 22:57
  • @TomJNowell no. It's actually the single property post, whereby I'm trying to display a "This property is sold" message if the post belongs to the "Sold" category.
    – RaajTram
    May 21 '16 at 22:59
  • Are you calling those functions inside or outside of the loop?
    – Tom J Nowell
    May 21 '16 at 23:01
  • @TomJNowell Inside of the loop. As I mentioned above - "Both are being used inside the loop (via a shortcode). Am I missing something?"
    – RaajTram
    May 21 '16 at 23:02
  • Does it change if you pass the 3rd argument to has_term?
    – Tom J Nowell
    May 22 '16 at 0:23
0

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="/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="/properties">View Exclusives</a></h3>
      </div>
<?php }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.