According to the WP_Class reference, $post->post_name holds the value of the slug of the current page being viewed. If I'm currently viewing the WP admin edit screen for my homepage as set under Settings > Reading > Front page displays > A static page > Front page, what would be the value of $post->post_name if I do the following?

global $post;
echo $post->post_name;

I wasn't aware my homepage had a slug. Would the value be an empty string? Hoping someone can help clarify. Thanks in advance.

  • 1
    I believe that reference to the $post object only applies on the front end. – vancoder Jun 27 '14 at 21:45
  • I think it applies to the edit screen as well. – henrywright Jun 27 '14 at 21:46
  • @vancoder on 2nd thoughts, I think you might be right – henrywright Jun 27 '14 at 21:57
  • @vancoder on 3rd thoughts, I think it does actually apply to the edit screen as well. – henrywright Jun 28 '14 at 1:48

post_name would be the slug as it was set before the page became the front page. When the URL for the front page is output via the API, the get_page_link function checks if the ID matches the page_on_front option and returns the home_url in that case. This is why you should always use the API to output data and not directly access post object fields.

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.