【发布时间】:2021-04-27 19:02:44
【问题描述】:
我在我的 WordPress 主题中编写了一个模块,它过于复杂且无关紧要。
我需要包含一段代码,并且需要一种可靠的方法来确定用户当前是否正在编辑特定页面。
本质上,我想要前端检查的等价物:
if (is_front_page()) {
但是,在编辑器页面上时。
还需要能够定位没有特殊名称的页面,例如首页/商店页面。所以我可能需要按帖子 ID 定位。
执行此检查的最简洁方法是什么?
我已经尝试了上面的功能,以及:
$screen = get_current_screen();
但是,这会导致严重错误:
Fatal error: Uncaught Error: Call to undefined function get_current_screen() in ... points to line.
还尝试了其他几种方法,我只记下一些。我以为我可以让一个人工作,但似乎没有人能做到这一点。请有人指出我正确的方向吗?
编辑 添加示例 sn -p
// This creates the fatal error.
// $screen = get_current_screen();
// var_dump($screen);
// This is for the front-end
// if (is_front_page()) {
// global $pagenow;
// var_dump($pagenow);
// if ($pagenow == 'post.php') {
// This returns string(8) "post.php" which is not descript enough.
// This is front-end
// global $post;
// $post_id = $post->ID;
// Post ID 3887 is Home
if ($post_id == '3887') {
echo 'hello!';
require_once('inc/overlays/page-template/fc-front-page.php');
} else {
echo 'Well that did not work!';
}
我这样做的原因无关紧要,我只是想在不同的文件夹中包含一个文件,调用的文件将根据当前正在编辑的页面而有所不同!
【问题讨论】:
-
您到底看到了什么严重错误?
-
@cabrerahector - 为您更新了问题:)
-
啊,我认为您通常无法从您的主题中访问管理功能,例如 get_current_screen(),除非您在主题中使用特定的挂钩,例如 admin_init。
-
话虽如此,如果您希望我们提供比这更多的帮助,请分享Minimal, Reproducible Example(并解释您为什么这样做),以便我们分享更多建议。跨度>
-
为您添加了该信息 :) 我喜欢 admin_init,但我仍然需要了解如何确定当前正在编辑哪个页面。
标签: php wordpress function wordpress-theming