WordPress get current page/post ID

Here how to get the current Page or Post ID in WordPress.

 

global $wp_query;
$current_page_id = $wp_query->get_queried_object_id()

or you can try this:

global $wp_query;
$current_page_id = $wp_query->post->ID;
if(is_home()){
    if('page' == get_option('show_on_front')){
        if(is_front_page()){
            $current_page_id = get_option('page_on_front');
        }else{
            $current_page_id = get_option('page_for_posts');
        }
    }
}

It should work as long as there is a page or post, works best if you use a static home page in blog “Reading” settings.

Leave a Reply

Your email address will not be published. Required fields are marked *