i need to get page ID in wordpress throught php?
A:
There is a global variable "$post
" which contains the related information of the current post / page, and is actually an object. You can access information just as you access variables from an object. Remember to keep it in the while loop.
For example, confider the following:-
<?php if (have_posts()) : ?>
<?php
while (have_posts()):
the_post();
global $post;
$idPagePost = $post->ID;
endwhile;
?>
<?php endif; ?>
Now the variable "$idPagePost" will contain the ID of the current page / post.
Hope it helps.
Knowledge Craving
2010-07-02 10:49:48
A:
global $wp_query;
$id = $wp_query->post->ID;
// OR:
$id = $wp_query->queried_object_id;
This will work anywhere in your themes or plugins, as long as it happens after WordPress is loaded.
John P Bloch
2010-07-02 14:13:34
I should add that both of these will work anywhere on singular pages (single blog, static page, etc.), but you should use the first for multi-view pages inside the loop. The second will only work on singular pages, while the first will only give you the first returned post on multi-post views, unless you're using it in the loop.
John P Bloch
2010-07-02 14:16:47