php - WordPress 如果 post.php 但仅适用于一种自定义帖子类型
问题描述
这是针对后端的。管理区域。我有一个脚本来运行一些功能,但仅适用于由事件日历创建的事件自定义帖子类型。目前我在我的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
全局变量,但老实说,我没有看到它使用太多。
推荐阅读
- java - 如何通过zuul api-gateway登录客户端UI
- java - 通过pdfbox替换字符串后获取非法字符
- function - 如何使用 scilab 在矩阵中保存循环生成的输出参数函数?
- output - 如何从 FiPy 输出 .pvd 或 xdmf 文件
- php - 来自两个逗号分隔的字符串 PHP 的 foreach 值
- django - 合并了 2 个 django 项目的权限。如何将它们分开?
- python - 在 tf.keras 中定义模型块
- php - 如何在我的搜索中添加 where 条件?
- c++ - 正确写一个类和使用pthread配合vlc库和c++
- javascript - 使用 JavaScript 混淆文本?