最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

permalinks - Return value of get_permalink(0) and get_the_title(0)

programmeradmin3浏览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)

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)

Share Improve this question asked Aug 23, 2019 at 8:18 RugoRugo 1031 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 2

If 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.

发布评论

评论列表(0)

  1. 暂无评论