Check if current Download is in a specific category

326 Views Asked by At

So I'm working with the sinlge-download.php page and I'm trying to check if the specific product is in a specific category. Here is what I tried but I only get the ELSE result even if the download is a book.

if( in_category( 'Books' ) ) {
    echo 'This product is a book';
   } else {
    echo 'This product is not a book';
   }
2

There are 2 best solutions below

0
Howard E On BEST ANSWER

According to EDD docs, the category is: download_category Easy Digital Download Docs

For this... use function has_term since in_category refers to WordPress post type posts and not for custom post types like the downloads.

if( has_term( 'Books', 'download_category' ) ) {
    echo 'This product is a book';
} else {
    echo 'This product is not a book';
}
0
Kay On

You can use this

if( has_term( $term = '', $taxonomy = '', $post = null ) ) {
    // do something
}

// $term = Category OR Taxonomy name $taxonomy = Taxonomy Name. OR
// "category" if its default WP category $post = Post ID to check. Leave
// empty to pull this from global query

https://developer.wordpress.org/reference/functions/has_term/