这是为了后端。管理区域。我有一个脚本来运行一些功能,但仅限于事件日历创建的事件自定义帖子类型。目前我在我的functions.php中运行了这个脚本:
if ($page == "post-new.php" OR $page == "post.php" ) {
do something.....;
}
所以它有效,但对于post-new.php或post.php。不好,因为这涉及仅与事件CPT相关的字段。我怎样才能让它专门用于此CPT?
通常,您可以依靠get_current_screen()
函数返回您所在的管理页面的数据:
$current_screen = get_current_screen();
if( $current_screen->base == 'post' && $current_screen->post_type == 'your-post-type-here' ){
// Do things
}
还有$post_type
Global Variable但老实说,我不认为它用得太多了。