0

Is there any experience using widget logic, I want to display a widget on single custom post type with specific taxonomy. Tried using

is_tax(taxonomy_name) || is_single() && in_term(term_name).
12
  • I don't know what errors you are getting or what exactly your issue is, but in_term() according to my knowledge is invalid Commented Jun 22, 2015 at 11:11
  • Hi Pieter, the error I got is the sidebar widget not showing anything.
    – da_root
    Commented Jun 22, 2015 at 11:13
  • So, what the conditional should be for the cuatom taxonomy? As per I knew, in_category is only for default
    – da_root
    Commented Jun 22, 2015 at 11:27
  • Do you mean a specific term, or for all terms under a specific taxonomy Commented Jun 22, 2015 at 11:29
  • Yes :), for example I have custom taxonomy with name resources, and child taxonomy of the resources is economic_news so I want to display the widget only on the single page of that taxonomy.
    – da_root
    Commented Jun 22, 2015 at 11:32

2 Answers 2

1

Before I start, I must say that your terminology is quite confusing and plain wrong. You should take your time and read through my answer to this question: Is There a Difference Between Taxonomies and Categories?

As I stated before, there is no in_term() function to check if a post has a specific term. There is however a has_term() function which accepts the term as first parameter and the taxonomy name as second parameter. So your condition should look like this: (if this is for a specific post type outside the loop)

global $post;
if ( $post->post_type == 'my_post_type' // checks the post type of the post
     && is_single() // Checks if this is a single post
     && has_term( 'term-name or id or slug', 'my_taxonomy', $post->ID ) // Check if post has specific term
) {
    // Do something if our condition is true
}
-1

Please try below code:

if(has_tag( array( 'sharp', 'mild', 'extreme' ) ) && get_post_type()=="custom-post-type"){
}

The array value should be the term name which is stored in taxonomy.

You can directly pass the array in has_tag() function

5
  • has_tag() will not work. It works for the build in taxonomy post_tag, not a custom taxonomy Commented Jun 22, 2015 at 12:23
  • Also note, there is no gas_tag() function. I have corrected that Commented Jun 22, 2015 at 12:25
  • i have write for has_tag() function not gas_tag() function.
    – Anand
    Commented Jun 22, 2015 at 13:12
  • Whatever. Please see the revision history if you think I'm lying. As I said, I have corrected it Commented Jun 22, 2015 at 13:14
  • please check this if(in_array('tag-name',get_the_tags()) && get_post_type()=="custom-post-type")
    – Anand
    Commented Jun 22, 2015 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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