wordpress plugin: query post-ID in plugin?

ε祈祈猫儿з 提交于 2019-12-02 09:18:10

My guess is Use global keyword to access post id in function

And also my guess is return and echo both would not work together in function

function test($content) {
        global $post;
        return $post->ID.'<br>'.$content;
    }

You're mixing echo and return - that doesnt work. However, try:

function test($content) 
{
    return "id: ".$post_id."<br/>".$content;
}

also, make sure to use lowercase id, as it is case-sensitive

http://codex.wordpress.org/Function_Reference/get_the_ID might be usefull aswell

Filters should return, not echo.

function test($content) {
    global $post;
    return 'id: ' . $post->ID . '<br />' . $content;
}

In order to look at the post object properties you must bring $post into the scope of the function, that's what this line does..

global $post;

Which then allows the reference to the object's ID, eg.

$post->ID;

See here for help understanding actions and filters.
http://codex.wordpress.org/Plugin_API

Example filter.
http://codex.wordpress.org/Plugin_API#Example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!