1

I use WordPress Static Front Page Process to assign the Front Page and assign the Posts Page. So I have 2 pages : "front" (front-page.php) and "blog" (home.php).

For the "blog" page, When I display $post->ID after head init (outside the loop), I don't understand why WordPress send me back the last post ID created. It normally has to display the page ID?

I read the "Questions that may already have your answer" about this kind of case and the ability to use :

get_option('page_for_posts')
  • But why Wordpress works like this?

  • Is there a global var defined?

  • Is there something I'm missing in WordPress Template Hierarchy?

Thanks for your help

  • You have the loop for the blog posts and that's probably what is causing the trouble. Usually I never define the blog page as the default blog template with WordPress, so I don't know how to properly solve that, but have you tried using global $post before the $post->ID ? – Marcelo Henriques Cortez Nov 28 '17 at 10:59
  • Thanks Marcelo. I used global $post->ID outside the loop, just after head init. – Didav Nov 28 '17 at 11:07
  • Glad it worked =) I'll make an answer with that so you can accept it – Marcelo Henriques Cortez Nov 28 '17 at 11:08
  • Sorry Marcelo, you don't understand. Your answer doen't help me. It was just a precision. Has said in my original question i use global $post->ID outside the loop, just after head init – Didav Nov 28 '17 at 11:19
  • Oh, ok, got it. Well, I really can't think what the problem is, since I usually do it other way. Hope someone can shed some light then. – Marcelo Henriques Cortez Nov 28 '17 at 11:23
1

The $post object contains data for the main query of the current page. So in your case the main query on your front-page.php contains the data from the page you assign to be your front-page. Your blog page however is an archive and can contain multiple post data. And in both cases you use the loop to access this data.

Maybe this page will help explain some more:

https://developer.wordpress.org/themes/basics/the-loop/

Edit:

Okay so I think what you need is get_queried_object() this will return the Page ID when on the front-page or blog. The $post object contains data for the first post in the Loop, you should think of the Blog page as an Archive instead of a Page.

  • Sorry Nath, i understand what you said, but Blog page is not an archive. It's a page, that use normally Blog Post Index page request in [WordPress Template hierachy]developer.wordpress.org/files/2014/10/wp-hierarchy.png) I know how to use data and the loop, or loop thru another WP_Query. So it's not my problem... It's the concept that looks weird... My question behavior surely come from WP_Query() default parameters. – Didav Nov 28 '17 at 11:15
  • I think you still don't understand what the $post object is and I think what you are after is the get_queried_object() function. I'll update my answer to try and help. – ngearing Nov 29 '17 at 1:49

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.