[wp-hackers] How to make $post->ID global
David Law
wp-hackers at google-adsense-templates.co.uk
Fri May 11 01:28:33 UTC 2012
Trying to use the post id in an included php file which is basically an extension of functions.php (tried the code in functions.php as well with no success).
Examples of what I've tried
global $post;
$postsid = $post->ID;
echo 'test '.$postsid;
and
global $wp_query;
$postsid = $wp_query->post->ID;
echo 'test '.$postsid;
This fails when added to the included file (and functions.php), works when added to header.php.
Have tried global $postsid; when using $postsid
How do you generate the post id for the post/page you are on?
The above is test code which I resorted to as couldn't get the post id for a function that uses code like this:
global $post;
$stmeta2011headeror = get_post_meta($post->ID, 'st_meta_2011header_or', true);
$post->ID isn't outputing anything if it's not within a template file like header.php. Replacing $post->ID with a posts id and loading that post works.
Seen quite a few support requests in the WordPress forums, but the general advice is add global $post; which I've tried.
Probably not relevant to the problem, but this is what I'm trying to do (detailed info)
Have a theme with a customized version of the TwentyEleven random header image code, basically rather than one set of 8 header images there's multiple sets of images that are selected via an options page (had this working for a while).
Trying to setup a custom field on post and page edit screens that allows the user to override the main header set (set on an options page) on a post by post basis. If I can get it to work there will be an option to select any of the sets that are part of the theme or child theme (this code is working, checks installed headers on the parent theme and child theme) meaning every post can have a different header set available to it when the random rotating feature is active (choose a header set and one of eight images are loaded at random from that set on that page only). I have this concept working for similar image features and a layout and colour scheme (the theme has 13 layouts and almost 30 colour schemes, every post can have a different layout and or colour scheme, it's working perfectly), but for the TwentyEleven code I'm having problems getting the post id within the code.
I have the code for the TwentyEleven header image code within an include file rather than directly within the functions.php file (though testing in functions.php failed as well).
Trying to use code like this within the include file
global $post;
$stmeta2011headeror = get_post_meta($post->ID, 'st_meta_2011header_or', true);
But $post->ID isn't generating the ID, replacing $post->ID with a posts ID (1 for example) results in the code working as expected when viewing post 1, so the only problem is obtaining the current posts ID (which should be easy) on an included PHP file.
Using this code within header.php
$stmeta2011headeror = get_post_meta($post->ID, 'st_meta_2011header_or', true);
echo $stmeta2011headeror;
Outputs the posts meta field entry.
I've tried
$post->ID
$wp_query->post->ID
get_the_ID()
Confused :-)
David
More information about the wp-hackers
mailing list