views:

23

answers:

4

i need to get page ID in wordpress throught php?

+1  A: 

You want to use the_ID() within the loop.

tschaible
+1  A: 

Assuming this is for a Theme, it's as simple as this.

OJ
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
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
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