Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Wordpress管理员中获取帖子ID_Php_Wordpress - Fatal编程技术网

Php 如何在Wordpress管理员中获取帖子ID

Php 如何在Wordpress管理员中获取帖子ID,php,wordpress,Php,Wordpress,我正在开发Wordpress插件,我需要获取当前的帖子ID 在写文章/写页面编辑屏幕中(循环外) 我还需要在“admin\u print\u scripts”钩子之前执行此操作,因为我希望将一些数据传递给javascript文件 我不能使用: $id = $_GET['post']; 因为url在添加新帖子或页面时不包含此变量 到目前为止,我已经尝试过这些选项,但都不起作用: A) 这将返回一个ID 0 function myplugin_setup() { global $wp_qu

我正在开发Wordpress插件,我需要获取当前的帖子ID
写文章/写页面编辑屏幕中(循环外)

我还需要在“admin\u print\u scripts”钩子之前执行此操作,因为我希望将一些数据传递给javascript文件

我不能使用:

$id = $_GET['post'];
因为url在添加新帖子或页面时不包含此变量

到目前为止,我已经尝试过这些选项,但都不起作用:

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) 这将返回一个null的ID

function myplugin_setup() {
    global $wp_query;
    $id = $wp_query->post->ID;
    var_dump($id);
}

add_action('admin_init', 'myplugin_setup' );
function myplugin_setup() {
    global $post;
    $id = $post->ID;
    var_dump($id);
}

add_action('admin_init', 'myplugin_setup' );
C) 这还返回一个null的ID

function myplugin_setup() {
    global $wp_query;
    $id = $wp_query->post->ID;
    var_dump($id);
}

add_action('admin_init', 'myplugin_setup' );
function myplugin_setup() {
    global $post;
    $id = $post->ID;
    var_dump($id);
}

add_action('admin_init', 'myplugin_setup' );

如果它是一个新的帖子/页面,我猜id还不存在,因为帖子还没有发布/添加到DB中。如果您试图编辑帖子/页面,我认为您可以使用
$id=$\u GET['post']

使用:

global $post

在您的功能开始时。然后,您应该可以访问$post->ID以获取当前帖子的ID。这将适用于新的和现有的帖子。

问题是,您正在使用admin\u init hook。如果您查看操作引用--您将看到,这个钩子实际上是在查询帖子之前调用的,这就是为什么您使用的变量还没有被填充的原因


您可以使用一些稍后的操作(使用一些is_admin()检查),或者您可以使用admin init hook将操作添加到一些稍后的钩子中,因此它将仅用于admin。

确保在WordPress查询之后调用全局$post。如果向init或admin_init添加操作,则查询尚未就绪,因此无法从全局$post变量中获得任何信息

我的建议是检查本页的操作参考:并选择一个适合您的

例如,我这样做:

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

    }
}
我能够在WP admin中获取页面ID,这可能会起作用:

$id = get_the_ID();

当用户访问管理区域时,“admin_init”操作在任何其他挂钩之前触发。它是在新帖子获得id之前触发的

要获取新的帖子id,您可以使用“save_post”,这是一个在创建或更新帖子或页面时触发的操作()

首先,您可以使用“admin_enqueue_scripts”包含脚本,然后使用“save_post”获取新的帖子id。“admin_print_scripts”在“save_post”之后触发,您可以使用wp_localize_script()或其他方法将新帖子id传递给javascript

我需要类似的东西,但在课堂上使用过

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;
    }
}
现在,在javascript中,您可以使用:

myJsObject.postId

对于仍然想知道要调用的正确钩子,或者wordpress管理生命周期中的众多钩子之一的人来说,是:

详情如下:

function myplugin_setup() {
    global $post;
    $id = $post->ID;
    var_dump($id);
}

add_action('admin_head', 'myplugin_setup' );

你想这样做吗:事实上,每次你请求新帖子/页面屏幕时,wordpress都会在数据库中插入一篇新帖子,帖子状态为“自动草稿”。在这种情况下,如何获取自动草稿帖子ID?我需要为新帖子预先定义一些帖子的元值。@Ciantic你可以用与普通帖子完全相同的方法获得autodraft的ID。每次打开“添加帖子/页面”时,都会在数据库中创建一条新记录,然后页面将以空值呈现,因此,从技术上讲,在页面呈现时,您将有一个标准的全局$post变量。@但您可能对_post操作感兴趣:-但是在回调中,您应该检查它是否是新创建的页面:全局$pagenow$newMode=$pagenow=='post new.php';请参见@Cippo,使用
global$post
的函数/方法需要在
$post
可用后挂接,将函数挂接到
管理头
工作