获取循环外的当前帖子id.Wordpress


Get current post id outside the loop. Wordpress

我正在为wordpress制作一个插件。问题是,我如何在循环之外获得当前的帖子ID?

我在帖子上有一个按钮,当用户单击该按钮时,应该返回post ID

那么,无论如何都要获得帖子id吗?

我的插件目录:wp-content'plugins'updateDatabase'js'connectionDatabase.php

这是我的代码:和我的网址http://localhost:8080/wordpress/?p=1169

        $post_id = $_GET['p'];
        echo $post_id;
        $link = getConnection();
        $bonusPoint = 0;
        $query_sql = "SELECT bonus_point FROM post_data03 WHERE post_id =" .$post_id;
        $result = mysqli_query($link, $query_sql);
        if (mysqli_num_rows($result) > 0) {
                // output data of each row
                while($row = mysqli_fetch_assoc($result)) {
                    $bonusPoint = $row['bonus_point'];
                }
       } else {
                  echo "0 results";
       }

使用$post:

function getBonusPointFromDataPost()
{
        global $wp_query; 
        global $post;
        $post_id = add_action( 'the_post', 'get_post_info' );
        $link = getConnection();
}

当用户点击帖子页面上的按钮时,GetBonusPointFromDataPost()将被触发。同时,应返回$post_id

我制作了一个函数来返回$post_id,但它只有在我向其中添加action时才起作用

function get_post_info() {
        global $wp_query;
        global $post;
        $post_id = $wp_query->post->ID;
        echo $post_id;
        return  $post_id;
    }

当用户单击按钮时,如何返回$post_id

就在这里,使用global $post获取当前post对象

global $post;
echo $post->ID;