php 在wordpress插件页面中获取当前页面ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22351038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:51:27  来源:igfitidea点击:

get the current page id inside wordpress plugin page

phpwordpress

提问by Sagin

I need to get the current page idin WordPress plugin page outside the loop. And the code I wrote for getting current page idis in my plugin page. I tried many codes, but doesn't work

我需要page id在循环外获取WordPress 插件页面中的当前内容。我为获取最新信息而编写的代码page id在我的插件页面中。我尝试了很多代码,但不起作用

$page_object = get_queried_object();
$page_id     = get_queried_object_id();


 // "Dirty" pre 3.1
 global $wp_query;

$page_object = $wp_query->get_queried_object();
$page_id     = $wp_query->get_queried_object_id();

But it doesn't work for me .

但这对我不起作用。

  global $post;
  echo "pageid: ".$post->ID;

This is also not working.

这也行不通。

When I try

当我尝试

     global $wp_query;
     $post_obj = $wp_query->get_queried_object();
     $Page_ID = $post_obj->ID;
     echo $Page_ID;

Then a error message appears

然后出现错误信息

Fatal error: Call to a member function get_queried_object() on a non-object in H:\xampp\htdocs\wordpress\wp-content\plugins\wpk\wpk.php on line 876

致命错误:在第 876 行的 H:\xampp\htdocs\wordpress\wp-content\plugins\wpk\wpk.php 中的非对象上调用成员函数 get_queried_object()

When I print:

当我打印时:

global $wp_query;
print_r($wp_query);

then result is:

那么结果是:

WP_Query Object
(
    [query] => 
    [query_vars] => Array
        (
        )

    [tax_query] => 
    [meta_query] => 
    [date_query] => 
    [queried_object] => 
    [queried_object_id] => 
    [request] => 
    [posts] => 
    [post_count] => 0
    [current_post] => -1
    [in_the_loop] => 
    [post] => 
    [comments] => 
    [comment_count] => 0
    [current_comment] => -1
    [comment] => 
    [found_posts] => 0
    [max_num_pages] => 0
    [max_num_comment_pages] => 0
    [is_single] => 
    [is_preview] => 
    [is_page] => 
    [is_archive] => 
    [is_date] => 
    [is_year] => 
    [is_month] => 
    [is_day] => 
    [is_time] => 
    [is_author] => 
    [is_category] => 
    [is_tag] => 
    [is_tax] => 
    [is_search] => 
    [is_feed] => 
    [is_comment_feed] => 
    [is_trackback] => 
    [is_home] => 
    [is_404] => 
    [is_comments_popup] => 
    [is_paged] => 
    [is_admin] => 
    [is_attachment] => 
    [is_singular] => 
    [is_robots] => 
    [is_posts_page] => 
    [is_post_type_archive] => 
    [query_vars_hash] => 
    [query_vars_changed] => 1
    [thumbnails_cached] => 
    [stopwords:WP_Query:private] => 
)

I don't know how to solve this, how to get the current page id.If you know how to solve this, then I need your support. Thanks in advance.

我不知道如何解决这个问题,如何获得电流page id。如果你知道如何解决这个问题,那么我需要你的支持。提前致谢。

回答by Sagin

get_the_ID(); or $post->ID;returns the current page or post id in Wordpress.

get_the_ID(); or $post->ID;返回 Wordpress 中的当前页面或帖子 ID。

But you need to ensure that your post is saved in wordpress post table. Other wise you can'tget the id , simply because of it is not an entry in wordpress database.

但是您需要确保您的帖子保存在 wordpress 帖子表中。否则,您将无法获得 id ,因为它不是 wordpress 数据库中的条目。

If it is a static page and it's not an entry in wordpress post then, get_the_ID()didn't return anything.

如果它是一个静态页面并且它不是 wordpress 帖子中的条目,则get_the_ID()不会返回任何内容。

For example: get_the_ID() didn't go to work in post archive pages , administration pages in wordpress backend etc.

例如:get_the_ID() 在帖子归档页面、wordpress 后端的管理页面等中不起作用。

So as per this questionyou are trying to get the id of the page that is a backend plugin setting page or any archive page .

因此,根据此问题,您正在尝试获取作为后端插件设置页面或任何存档页面的页面的 ID。

UPDATE

更新

Method to get the current post id in wordpress

wordpress中获取当前帖子id的方法

(1) global $post; $post->ID();

(1) global $post; $post->ID();

(2) global $wp_query; $post_id = $wp_query->get_queried_object_id();

(2) global $wp_query; $post_id = $wp_query->get_queried_object_id();

(3) global $wp_query; $post_id = $wp_query->post->ID;

(3) global $wp_query; $post_id = $wp_query->post->ID;

(4) get_the_ID();

(4) get_the_ID();

[ It is recommended that this tag must be within The Loop. ]

[ 建议此标签必须在 The Loop 内。]

see this

看到这个

      function get_the_ID() {
               $post = get_post();
               return ! empty( $post ) ? $post->ID : false;
                }

ie get_the_ID() return the id of current $post .

即 get_the_ID() 返回当前 $post 的 ID。

(5) get_query_var('page_id')

(5) get_query_var('page_id')

[ it will not going to work if we use pretty permalink ]
https://codex.wordpress.org/Function_Reference/get_query_var

[如果我们使用漂亮的永久链接,它将无法工作]
https://codex.wordpress.org/Function_Reference/get_query_var

回答by Rajalakshmi

You can get IDof the post in current page outside the loop using the technique below:

您可以ID使用以下技术在循环外获取当前页面中的帖子:

global $wp_query;
$post_id = $wp_query->post->ID;

$post = get_post( $post_id );
$slug = $post->post_name;

回答by Ram Sharma

try to use below code to get the page id

尝试使用下面的代码来获取页面 ID

get_the_ID();

回答by Dreszczyk

Chosen answer is working only if you put it in the Wordpress loop. Outside it will render useless.

选择的答案仅在您将其放入 Wordpress 循环中时才有效。在外面它会变得毫无用处。

This is working everywhere:

这在任何地方都有效:

global $wp_query;
$postID = $wp_query->post->ID;

回答by D.A.H

I guess that this is the proper solution:

我想这是正确的解决方案:

$id = get_queried_object_id();

which equals to:

这等于:

function get_queried_object_id() {
    global $wp_query;
    return $wp_query->get_queried_object_id();
}

回答by Brane

You get see all of the settings and variables in get_defined_vars()function:

您可以看到get_defined_vars()函数中的所有设置和变量:

var_dump(get_defined_vars());

In your case you need to get '_GET' and inside 'post'... The code should look like this:

在您的情况下,您需要获取 '_GET' 并在 'post' 内...代码应如下所示:

$tmp = get_defined_vars();
var_dump($tmp['_GET']['post']);