WordPress本地环境自定义插件无法获取当前页面ID求助
解决WordPress插件中无法获取当前页面ID的问题
问题情况
在自定义WordPress插件中尝试获取当前页面ID时,始终返回null或false。打印$wp_query的结果显示in_the_loop为false,所有页面相关属性均为空:
/srv/www/wordpress/wp-content/plugins/blueprint/partials/helpers.php:5: object(WP_Query)[553] public 'query' => null public 'query_vars' => array (size=0) empty public 'tax_query' => null public 'meta_query' => boolean false public 'date_query' => boolean false public 'queried_object' => null public 'queried_object_id' => null public 'request' => null public 'posts' => null public 'post_count' => int 0 public 'current_post' => int -1 public 'in_the_loop' => boolean false public 'post' => null public 'comments' => null public 'comment_count' => int 0 public 'current_comment' => int -1 public 'comment' => null public 'found_posts' => int 0 public 'max_num_pages' => int 0 public 'max_num_comment_pages' => int 0 public 'is_single' => boolean false public 'is_preview' => boolean false public 'is_page' => boolean false public 'is_archive' => boolean false public 'is_date' => boolean false public 'is_year' => boolean false public 'is_month' => boolean false public 'is_day' => boolean false public 'is_time' => boolean false public 'is_author' => boolean false public 'is_category' => boolean false public 'is_tag' => boolean false public 'is_tax' => boolean false public 'is_search' => boolean false public 'is_feed' => boolean false public 'is_comment_feed' => boolean false public 'is_trackback' => boolean false public 'is_home' => boolean false public 'is_privacy_policy' => boolean false public 'is_404' => boolean false public 'is_embed' => boolean false public 'is_paged' => boolean false public 'is_admin' => boolean false public 'is_attachment' => boolean false public 'is_singular' => boolean false public 'is_robots' => boolean false public 'is_favicon' => boolean false public 'is_posts_page' => boolean false public 'is_post_type_archive' => boolean false private 'query_vars_hash' => boolean false private 'query_vars_changed' => boolean true public 'thumbnails_cached' => boolean false private 'stopwords' => null private 'compat_fields' => array (size=2) 0 => string 'query_vars_hash' (length=15) 1 => string 'query_vars_changed' (length=18) private 'compat_methods' => array (size=2) 0 => string 'init_query_flags' (length=16) 1 => string 'parse_tax_query' (length=15)
当前使用的代码是:
<?php add_action('init','newFunction'); function newFunction(){ global $wp_query; var_dump($wp_query); // Do your code here }
核心需求是获取当前页面ID,判断是否在指定ID列表中,从而决定是否渲染插件内容。
问题原因
init钩子触发时机过早,此时WordPress还未完成主查询的初始化,$wp_query对象还未填充页面相关数据,自然无法获取到页面ID。
解决方案
1. 替换为合适的钩子
选择在主查询完成后触发的钩子,比如wp或template_redirect:
<?php // 使用wp钩子 add_action('wp', 'newFunction'); function newFunction(){ $page_id = get_queried_object_id(); // 检查页面ID是否在列表中 $target_pages = [1, 2, 3]; // 替换为你的页面ID列表 if (in_array($page_id, $target_pages)) { // 渲染插件内容 echo '插件内容'; } }
或者使用template_redirect钩子:
<?php add_action('template_redirect', 'newFunction'); function newFunction(){ global $post; if ($post) { $page_id = $post->ID; $target_pages = [1, 2, 3]; if (in_array($page_id, $target_pages)) { // 执行渲染逻辑 } } }
2. 直接使用get_queried_object_id()函数
该函数无需依赖全局变量,可直接返回当前查询对象的ID,在主查询完成后调用即可:
$page_id = get_queried_object_id(); if ($page_id && in_array($page_id, $target_pages)) { // 执行插件渲染操作 }
3. 后台页面的ID判断(如果需要)
如果要在WordPress后台判断页面ID,可结合get_current_screen()函数:
add_action('admin_init', 'check_admin_page'); function check_admin_page(){ $screen = get_current_screen(); if ($screen && $screen->base === 'post' && $screen->post_type === 'page') { $page_id = $_GET['post'] ?? 0; $target_pages = [1,2,3]; if (in_array($page_id, $target_pages)) { // 后台相关逻辑 } } }
内容的提问来源于stack exchange,提问作者Muhammad Fahad
相关产品推荐
相关产品推荐

