最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

globals - Current page id returns the incorrect value

programmeradmin10浏览0评论

I need to capture the current page id, execute some conditional script in footer, in my own plugin function hooks to wp_footer.

Here is my plugin code, but page id returning wrong value?

<?php
/*
  Plugin Name: My Plugin
  Author:  GS
  Version: 1.1
*/
Function footer_page_id()
{
  global $post;
  echo '<div> page_id:'.$post->ID.'</div>';// output wrong value
  global $wp_query;      
  echo 'page_id:'.get_the_ID();// output wrong value
  echo 'post_id:'.$wp_query->post->ID; // output wrong value
  echo 'page_id:'.$wp_query->get_queried_object_id(); // output always 0
  echo'</br>';exit;
}
 add_action('wp_footer', ' footer_page_id ');
?>

Thanks in advance.

I need to capture the current page id, execute some conditional script in footer, in my own plugin function hooks to wp_footer.

Here is my plugin code, but page id returning wrong value?

<?php
/*
  Plugin Name: My Plugin
  Author:  GS
  Version: 1.1
*/
Function footer_page_id()
{
  global $post;
  echo '<div> page_id:'.$post->ID.'</div>';// output wrong value
  global $wp_query;      
  echo 'page_id:'.get_the_ID();// output wrong value
  echo 'post_id:'.$wp_query->post->ID; // output wrong value
  echo 'page_id:'.$wp_query->get_queried_object_id(); // output always 0
  echo'</br>';exit;
}
 add_action('wp_footer', ' footer_page_id ');
?>

Thanks in advance.

Share Improve this question edited Sep 26, 2018 at 11:13 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Sep 26, 2018 at 10:14 sridharnethasridharnetha 1011 silver badge4 bronze badges 1
  • 1 You may need to reset the $wp_query — try this: add wp_reset_query(); after the global $post;. – Sally CJ Commented Sep 26, 2018 at 10:29
Add a comment  | 

2 Answers 2

Reset to default 1

To get the current page ID outside of The Loop, you need to use get_queried_object_id(); ($wp_query-> is unnecessary).

However, this will only work when you are viewing a single Page (so anything under Pages > All Pages in the back-end).

If you are viewing the blog, an archive, or a category it will not be a valid page ID. On categories it will return the category ID, but on archives or the blog it will not have a value because those things do not have an ID.

If your theme or another plugin uses query_posts(), this could interfere with this function. Using wp_reset_query() will fix this, but whichever theme or plugin is running query_posts() should be doing this.

If you are using query_posts(), don't. Use a custom query with new WP_Query() or get_posts().

for above requirement you can rewrite the code like this

function footer_page_id()
{
  global $post;
  echo '<div> page_id:'.$post->ID.'</div>';
 // remaining codes here

}
 add_action('wp_footer', 'footer_page_id');

Please note that inorder to check the value of a variable inside a wordpress function please use debug log technique.

First enable debugging as per wordpress official tutorial https://codex.wordpress/Debugging_in_WordPress

Then you can find debugg.log file inside wp-content folder

function footer_page_id()
{
  global $post;
  echo '<div> page_id:'.$post->ID.'</div>';// output wrong value
  error_log(print_r($post->ID,true));


}
 add_action('wp_footer', 'footer_page_id');

Like this you can check the variable values in plugin.

发布评论

评论列表(0)

  1. 暂无评论