has_term or in_category for Custom Post Types

admin2025-01-07  5

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?

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?

Share Improve this question edited May 21, 2016 at 22:32 RaajTram asked May 21, 2016 at 22:20 RaajTramRaajTram 1599 bronze badges 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 Commented May 21, 2016 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 Commented May 21, 2016 at 22:59
  • Are you calling those functions inside or outside of the loop? – Tom J Nowell Commented May 21, 2016 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 Commented May 21, 2016 at 23:02
  • Does it change if you pass the 3rd argument to has_term? – Tom J Nowell Commented May 22, 2016 at 0:23
 |  Show 1 more comment

1 Answer 1

Reset to default 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 }
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736253376a119.html

最新回复(0)