2

In my home.php file I have only this code:

<?php
    global $post;
    print_r($post->ID);
    die();
?>

This is printing the ID of the latest blog post, but what I want is to get the ID of the current page (the page I set as the Posts Page in Settings). It seems that I am already in "The Loop". How can I get the global $post object for the current page rather than the first post in the loop?

1

¿How about getting the ID from de configuration?

<?php $page_for_posts = get_option( 'page_for_posts' ); ?>

As the page you are looking for now displays the posts, you are in the loop for posts, not the loop for the page.

  • I guess that will work. It seems like there should be another way, but maybe not. – Gavin Aug 28 '18 at 8:03
  • 1
    Actually, I just discovered get_queried_object_id() which gets the current page's ID even when you're in the loop. That's what I was looking for. Your answer is technically correct also, so I'll leave it as the answer. Thanks. – Gavin Aug 28 '18 at 8:28
1

The answer I accepted is technically correct, but I wanted to follow up with another answer that is what I originally was looking for. The function get_queried_object_id() will get the page's ID even when you're within the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.