I have a wp_localize_script()
function in my functions.php where I need to push the current post ID of the current post to a jQuery file. So I was wondering how you can get the current post ID of a post in my functions.php.
These are both not working:
global $post;
$post_id = $post->ID;
global $wp_query;
$post_id = $wp_query->get_queried_object_id();
If you hook your localize script function to
wp_enqueue_scripts
, then you will have access to theglobal $post
variable. As long as you pick a hook at or after ‘wp’ you should have access to theglobal $post
.