最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - How can I see $post object in frontend from functions.php?

programmeradmin1浏览0评论

I would like to see the dump data of $post object in any page. So which code snippet to add in functions.php?

global $post;
print_r ($post);

This is not working. Please suggest.

I would like to see the dump data of $post object in any page. So which code snippet to add in functions.php?

global $post;
print_r ($post);

This is not working. Please suggest.

Share Improve this question asked May 3, 2020 at 13:29 Nayan ChowdhuryNayan Chowdhury 33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Adding:

global $post;
print_r ($post);

alone, won't work in functions.php (becuase WP's core functions etc wouldn't be loaded). It would inside a relevant template file, but not in functions.php.

To make it work in functions.php, you need to do it inside an function that is hooked to an appropriate hook. The "appropriate" hook may depend on what $post object you're looking for (e.g. that of a single post or a post type archive), but a good way to target many situations is the wp_head hook, e.g:

add_action('wp_head', function(){
    global $post;
    print_r ($post);
});
发布评论

评论列表(0)

  1. 暂无评论