wordpress get\u类别返回空

wordpress get\u类别返回空,wordpress,Wordpress,保存新注释后,我可以通过functions.php重定向 add_filter('comment_post','myredirect') 我想重定向到产品类别列表,例如: 所以我需要当前帖子的类别。无法从functions.php访问$post,因此从$GLOBALS获取了post_id $post_id = $GLOBALS[_POST][comment_post_ID]; // ok //据 //还尝试了wp_get_post_类别($post_id) 我得到一个空的$catego

保存新注释后,我可以通过functions.php重定向

add_filter('comment_post','myredirect')

我想重定向到产品类别列表,例如:

所以我需要当前帖子的类别。无法从functions.php访问$post,因此从$GLOBALS获取了post_id

$post_id = $GLOBALS[_POST][comment_post_ID];    // ok
//据

//还尝试了wp_get_post_类别($post_id)

我得到一个空的$categories

你能帮忙吗?
非常感谢

我通过使用来自


当我寻找产品而不是帖子时发现了它。:-)

许多人混淆了自定义帖子类型和使用类别的术语。如果您将术语设置为类别,例如“product\u cat”或其他内容,则您无法使用
get\u the\u category()
找到您正在执行的
get\u the \u terms($post\u id,'category')
这就是您无法找到它的原因。您必须首先找到您的帖子类型的术语,然后尝试使用
get\u terms($post\u id,'custom term')
$categories = get_the_category($post_id);

if ( ! empty( $categories ) ) {
    file_put_contents('testfile2.txt', esc_html( $categories[0]->name));
} else {
    file_put_contents('testfile2.txt', "NOTHING HERE");
}
$terms = get_the_terms( $post_id, 'product_cat' );
foreach ($terms as $term) {
    $product_cat_id = $term->slug;
    break;
}