获取wordpress插件页面内的当前页面ID [英] get the current page id inside wordpress plugin page

查看:124
本文介绍了获取wordpress插件页面内的当前页面ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

$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();

但这对我不起作用.

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

这也不起作用.

当我尝试

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

然后出现错误消息

致命错误:调用成员函数get_queried_object() 在一个非物体上 H:\ xampp \ htdocs \ wordpress \ wp-content \ plugins \ wpk \ wpk.php 在876行

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

当我打印时:

global $wp_query;
print_r($wp_query);

那么结果是:

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] => 
)

我不知道如何解决此问题,如何获得当前的page id.如果您知道如何解决此问题,那么我需要您的支持.预先感谢.

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.

推荐答案

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

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

但是您需要确保您的帖子保存在wordpress帖子中 桌子.否则,您无法获取ID,仅仅是因为它是 不是Wordpress数据库中的条目.

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

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

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

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

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

因此,按照这个问题 ,您正在尝试获取作为后端插件设置页面或任何存档页面的页面的ID.

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

更新

在wordpress中获取当前帖子ID的方法

Method to get the current post id in wordpress

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

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

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

(4)get_the_ID();

[建议此标签必须在The Loop中. ]

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

查看此

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

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

(5)get_query_var('page_id')

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

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

这篇关于获取wordpress插件页面内的当前页面ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆