首页 > 解决方案 > WordPress 如果 post.php 但仅适用于一种自定义帖子类型

问题描述

这是针对后端的。管理区域。我有一个脚本来运行一些功能,但仅适用于由事件日历创建的事件自定义帖子类型。目前我在我的functions.php中运行了这个脚本:

if ($page == "post-new.php" OR $page == "post.php" ) {
    do something.....;
}

所以它可以工作,但在 post-new.php 或 post.php 的任何东西上。不好,因为这涉及仅与事件 CPT 相关的字段。我怎样才能让它专门在这个 CPT 上工作?

标签: phpwordpressif-statementcustom-post-type

解决方案


通常,您可以依靠该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 全局变量,但老实说,我没有看到它使用太多。


推荐阅读