检查当前下载是否属于特定类别

Check if current Download is in a specific category

所以我正在使用 sinlge-download.php 页面,我正在尝试检查特定产品是否属于特定类别。 这是我尝试过的方法,但即使下载的是一本书,我也只能得到 ELSE 结果。

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

你可以用这个

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/

根据 EDD 文档,类别是:download_category Easy Digital Download Docs

为此...使用函数 has_term 因为 in_category 指的是 WordPress post 类型 posts 而不是自定义 post 类型,例如下载.

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