php 如何在 Wordpress Admin 中获取帖子 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8463126/
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
How to get Post ID in Wordpress Admin
提问by Victor
I'm developing a Wordpress plugin and I need to get the current Post ID
in the Write Post / Write Page editing screen(outside the loop).
我正在开发一个 Wordpress 插件,我需要
在“写帖子”/“写页面”编辑屏幕(循环外)中获取当前的帖子 ID。
I also need to do it before the "admin_print_scripts" hook, since I would like to pass some data to a javascript file.
我还需要在“admin_print_scripts”挂钩之前执行此操作,因为我想将一些数据传递给 javascript 文件。
I can't use:
我不能使用:
$id = $_GET['post'];
because the url doesn't include this variable when you are adding a new post or page.
因为当您添加新帖子或页面时 url 不包含此变量。
So far I've tried these options but none of them worked:
到目前为止,我已经尝试过这些选项,但都没有奏效:
A) This returns an ID of 0
A) 这将返回 ID 0
function myplugin_setup() {
global $wp_query;
$id = $wp_query->get_queried_object_id();
var_dump($id);
}
add_action('admin_init', 'myplugin_setup' );
B) This returns an ID of null
B) 这将返回一个空 ID
function myplugin_setup() {
global $wp_query;
$id = $wp_query->post->ID;
var_dump($id);
}
add_action('admin_init', 'myplugin_setup' );
C) This also returns an ID of null
C) 这也返回一个空 ID
function myplugin_setup() {
global $post;
$id = $post->ID;
var_dump($id);
}
add_action('admin_init', 'myplugin_setup' );
回答by Ciprian Tepes
Make sure you call the global $post after the WordPress query. If you add an action to init or admin_init, the query isn't ready so there's nothing you can get out of the global $post variable.
确保在 WordPress 查询后调用全局 $post。如果您向 init 或 admin_init 添加操作,则查询尚未准备好,因此您无法从全局 $post 变量中获得任何信息。
My advice would be to check the action reference from this page: http://codex.wordpress.org/Plugin_API/Action_Referenceand choose one that works for you.
我的建议是检查此页面中的操作参考:http: //codex.wordpress.org/Plugin_API/Action_Reference并选择一个适合您的操作。
For example, I did this:
例如,我这样做了:
add_action( 'admin_head', 'check_page_template' );
function check_page_template() {
global $post;
if ( 'page-homepage.php' == get_post_meta( $post->ID, '_wp_page_template', true ) ) {
// The current page has the foobar template assigned
// do something
}
}
And I was able to get the page ID in WP admin
我能够在 WP admin 中获取页面 ID
回答by Caleb
Use:
用:
global $post
at the beginning of your function. You should then have access to $post->ID to get the id of the current post. This will work for new and existing posts.
在您的函数开始时。然后,您应该可以访问 $post->ID 以获取当前帖子的 ID。这将适用于新的和现有的职位。
回答by Tomá? Kapler
Problem is, you are using admin_init hook. If you look into Action Reference - http://codex.wordpress.org/Plugin_API/Action_Reference- you will see, that this hook is actually called BEFORE quering posts, that's why those variables you use are not yet filled.
问题是,您正在使用 admin_init 钩子。如果你查看 Action Reference - http://codex.wordpress.org/Plugin_API/Action_Reference- 你会看到,这个钩子实际上被称为 BEFORE 查询帖子,这就是你使用的那些变量尚未填充的原因。
You can use some later action (with some is_admin() check), or you may use admin init hook to add action to some later hook, so again it will be used only on admin.
您可以使用一些稍后的操作(通过一些 is_admin() 检查),或者您可以使用 admin init 钩子将操作添加到某个稍后的钩子中,因此它只会在 admin 上使用。
回答by Dan Fluture
The 'admin_init' action is triggered before any other hook when a user accesses the admin area. It is triggered before the new post gets an id.
当用户访问管理区域时,'admin_init' 操作在任何其他钩子之前触发。它在新帖子获得 id 之前触发。
To get the new post id you can use 'save_post', which is an action triggered whenever a post or page is created or updated (http://codex.wordpress.org/Plugin_API/Action_Reference/save_post).
要获取新的帖子 ID,您可以使用“save_post”,这是在创建或更新帖子或页面时触发的操作(http://codex.wordpress.org/Plugin_API/Action_Reference/save_post)。
First you can include your scripts using 'admin_enqueue_scripts', then use 'save_post' to get the new post id. The 'admin_print_scripts' is triggered after 'save_post' and you can use wp_localize_script (https://codex.wordpress.org/Function_Reference/wp_localize_script), or other method to pass the new post id to your javascript.
首先,您可以使用“admin_enqueue_scripts”包含您的脚本,然后使用“save_post”获取新的帖子 ID。'admin_print_scripts' 在 'save_post' 之后触发,您可以使用 wp_localize_script ( https://codex.wordpress.org/Function_Reference/wp_localize_script) 或其他方法将新的帖子 ID 传递给您的 javascript。
I needed something similar, but it was used in a class.
我需要类似的东西,但它是在课堂上使用的。
class Foo {
// this will hold the id of the new post
private $postId = null;
public function __construct()
{
// the actions are triggered in this order
add_action('admin_enqueue_scripts', array($this, 'EnqueueScripts'));
add_action('save_post', array($this, 'SavePost'));
add_action('admin_print_scripts', array($this, 'LocalizeScripts'));
}
// enqueue your scripts and set the last parameter($in_footer) to true
public function EnqueueScripts()
{
wp_enqueue_script('myJs', 'js/my.js', array('jquery'), false, true);
}
// use wp_localize_script to pass to your script the post id
public function LocalizeScripts()
{
wp_localize_script('myJs', 'myJsObject', array('postId'=>$this->GetPostId()));
}
// if $post_id is different from global post id you are in the write/create post page, else you are saving an existing post
public function SavePost($post_id)
{
if($post_id != $this->GetPostId())
{
$this->SetPostId($post_id);
}
}
private function GetPostId()
{
if (!$this->postId) {
global $post;
if($post)
{
$this->SetPostId($post->ID);
}
}
return $this->postId;
}
private function SetPostId($postId)
{
$this->postId = $postId;
}
}
Now, in your javascript you can use:
现在,在您的 javascript 中,您可以使用:
myJsObject.postId
回答by Mel Macaluso
For anyone still wondering the correct hook to call, or one of the many in the wordpress admin lifecycle, is: admin_head
对于仍然想知道要调用的正确钩子或 wordpress 管理生命周期中的众多钩子之一的任何人,是:admin_head
as follows:
如下:
function myplugin_setup() {
global $post;
$id = $post->ID;
var_dump($id);
}
add_action('admin_head', 'myplugin_setup' );
回答by bingjie2680
If it is a new post/page, I guess the id does not exists yet, because the post has not been published/added to DB. If you are trying to edit a post/page, I think you may use $id = $_GET['post']
;
如果它是一个新的帖子/页面,我猜这个 id 还不存在,因为该帖子尚未发布/添加到数据库。如果您正在尝试编辑帖子/页面,我认为您可以使用$id = $_GET['post']
;
回答by Muhammad Jawad Arshad
this may work :
这可能有效:
$id = get_the_ID();