开发者

how to find Page id inwordpress

开发者 https://www.devze.com 2023-01-06 06:48 出处:网络
i need to g开发者_如何学Cet page ID in wordpress throught php?You want to use the_ID() within the loop.Assuming this is for a Theme, it\'s as simple as this.There is a global variable \"$post\" which

i need to g开发者_如何学Cet page ID in wordpress throught php?


You want to use the_ID() within the loop.


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消