M
M
Mr Crabbz2015-09-24 17:27:12
WordPress
Mr Crabbz, 2015-09-24 17:27:12

Why is $post->ID suddenly "property of non-object"?

Hello.
I'm trying to add editor styles that will be connected dynamically depending on the id of the post being edited.
More or less like this:

function give_me_post_id(){
  global $post;
  return $post->ID;
}
add_editor_style( get_stylesheet_directory_uri() . '/css/editor-style-' . give_me_post_id() . '.css' );

That is, when we edit a post with id = 10, then, in theory, editor-style-10.css should be connected.
But for some reason I get this notice:
PHP Notice:  Trying to get property of non-object in /home/h59971/data/www/site.com/wp-content/themes/mytheme/functions.php on line 140

Why is $post not an object in this case? Like specified global $post; ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2015-09-24
@Punkie

Make it easier
UPD.
I'm not sure, but I think the $post variable is only defined on external pages. Not in admin. But in the admin panel, when editing a certain post, $_GET['post'] is always present. You can navigate through it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question