[wp-hackers] How to make $post->ID global
David Law
wp-hackers at google-adsense-templates.co.uk
Fri May 11 15:04:40 UTC 2012
Thanks for the idea, couldn't get it to work, but figured out why.
Since the code is within the TwentyEleven header image code which is
enclosed within add_action( 'after_setup_theme', .... I couldn't get the
post id in anyway within that code. Changing 'after_setup_theme' to
'wp_head' allowed my original code to work.
Time to check the implications of this change on child themes as it looks
like it was added so child themes could override the header images, probably
not an issue for my theme as I have a way for child themes to override
almost everything.
David
-----Original Message-----
From: Charles K. Clarkson
Sent: Friday, May 11, 2012 3:54 AM
To: wp-hackers at lists.automattic.com
Subject: Re: [wp-hackers] How to make $post->ID global
On 5/10/2012 8:28 PM, David Law wrote:
> 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 :-)
header.php runs after $post has been defined.
Your include file runs before $post is defined.
The trick to make your include file function is to add it or its code with
an action that takes place after $post has been defined.
If header.php is okay, use add_action( 'wp_head', 'your_function' ) where
your function runs the code you want to add to the theme header.php file.
HTH,
Charles Clarkson
--
Free Market Advocate
Mobile Home Investor
Programmer
I'm not really a smart person. I just play one on the Internet.
Stephenville, TX
http://twitter.com/CharlesClarkson
+1 (254) 434-2733
_______________________________________________
wp-hackers mailing list
wp-hackers at lists.automattic.com
http://lists.automattic.com/mailman/listinfo/wp-hackers
More information about the wp-hackers
mailing list