add_shortcode and Post ID
-
This is probably a stupid question (again), but the add_shortcode hook doesn’t seem to pass along a post ID variable to it’s callback function (which makes sense, I suppose, given the shortcode may not always to used ‘inside the loop’).
Assuming the shortcode IS always to be used ‘inside the loop’, what is the best way of getting the ID of the post the shortcode is being used in into my callback function? My shortcode function needs to look at the current post’s meta data for the information it needs to display.
I’ve managed to do it by declaring $post as global
global $post; $post_id = $post->ID;
But I’m not sure this is the best way of doing it, given the aversion many programmers have to using global variables.
Cheers,
a|x
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘add_shortcode and Post ID’ is closed to new replies.