【发布时间】:2017-06-20 05:43:14
【问题描述】:
保存新评论后,我可以通过functions.php重定向
add_filter('comment_post', 'myredirect');
我想重定向到产品类别列表,比如: http://example.org/baktec27/?product_cat=1a
所以我需要当前帖子的类别。无法从 functions.php 访问 $post,所以从 $GLOBALS 获取 post_id
$post_id = $GLOBALS[_POST][comment_post_ID]; // ok
//根据https://developer.wordpress.org/reference/functions/get_the_category/
// 也试过 wp_get_post_categories( $post_id )
$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");
}
我得到一个空的 $categories。
你能帮忙吗? 非常感谢!
【问题讨论】:
标签: wordpress