I'm new to wordpress and I'm following a tutorial right now but I don't understand wordpresses behaviour. I'm trying to change the title of a sidebar which lists the parentpage and it's childpages, this works fine but I don't understand why $parentID = wp_get_post_parent_id(get_the_ID()); echo get_permalink($parentID);
works even on the parent page, I echoed the result on the parent page and it returns 0 since the parent page doesnt have a parent, so why does this still work? Why does get_permalink(0)
get me to the parent page if I press the button on the parent page? It also gives me the Title even though it would be get_the_title(0)
I'm new to wordpress and I'm following a tutorial right now but I don't understand wordpresses behaviour. I'm trying to change the title of a sidebar which lists the parentpage and it's childpages, this works fine but I don't understand why $parentID = wp_get_post_parent_id(get_the_ID()); echo get_permalink($parentID);
works even on the parent page, I echoed the result on the parent page and it returns 0 since the parent page doesnt have a parent, so why does this still work? Why does get_permalink(0)
get me to the parent page if I press the button on the parent page? It also gives me the Title even though it would be get_the_title(0)
1 Answer
Reset to default 2If an ID is passed to get_the_title()
or get_permalink()
, they will use get_post()
internally to get a copy of the full post object with that ID. But if get_post()
is passed either nothing or a "falsy" value, like 0
, then it will return the current global $post
object. In the context of your code this is likely to be the current page.
So:
get_permalink( 0 );
Is equivalent to:
get_permalink( get_the_ID() );
Which is equivalent to:
get_permalink();
Because they are all referring to the current global $post
.