[wp-hackers] Finding post ID while in header

David Chait davebytes at comcast.net
Tue Mar 21 21:04:53 GMT 2006


just to reiterate: in both cases, if inside a function you'll need to 
declare $post or $posts as globals to access their contents...

-d

----- Original Message ----- 
From: "Mark Jaquith" <mark.wordpress at txfx.net>
To: <wp-hackers at lists.automattic.com>
Sent: Tuesday, March 21, 2006 4:00 PM
Subject: Re: [wp-hackers] Finding post ID while in header


| On Mar 21, 2006, at 3:18 PM, CaptSolo wrote:
|
| > Could you suggest how to find out what the post ID is while in header?
| > E.g., how to display a post ID (if this is a single post page) within
| > HTML comments.
|
| <?php if ( is_single() ) echo $posts[0]->ID; ?>
|
| OR
|
| <?php if ( is_single() ) { $post = $posts[0]; the_ID(); } ?>
|
| The second one will let you use most "in the loop" functions
| prematurely (i.e. before the loop) and doesn't, in my experience,
| interfere with the loop.  But if all you need to do is echo the ID,
| I'd go with the first one.
|
| --
| Mark Jaquith
| http://txfx.net/



More information about the wp-hackers mailing list