获取当前正在查看的帖子最可靠的方法是global $post
变量相反,您应该首先检查is_singular()
, 然后使用get_queried_object()
获取post对象,或get_queried_object_id()
只需获取ID即可。
if ( ! is_singular() ) {
return;
}
$post_id = get_queried_object_id();
$args = array(
\'post_type\' => \'resources\',
\'category__in\' => wp_get_post_categories( $post_id ),
\'posts_per_page\' => 3,
\'post__not_in\' => array( $post_id )
);
// etc.