Here’s a way to get the top page url:
$top_page_url = get_permalink( array_slice( get_ancestors( get_the_ID(), 'page' ) , -1 ) );
where get_ancestors()
returns an array containing all the parents (ID) of the given page. You can read more about it in the Codex here.
Here are various ways to get the last array item, but note that end()
doesn’t expects a function as an input – more about it in the PHP docs here.
Related Posts:
- Check if is on child-page of a particular page
- A check for if is parent page, if has children, if has grandchildren
- Can I have two child pages of different parents with the same name?
- Create a “Dummy” parent page for a hierarchy in page listing?
- Allow only new sub-pages to be created
- Is there a default template file for child pages / subpages?
- Some pages are missing from the Parent Page select in the Editor
- How to display list of child pages of parent page in wordpress?
- Only allow new subpages to be created
- Can’t change parent page
- Show child pages when on a child page
- select a private page as parent for another private page
- WordPress pages with hierarchy
- load common thank you template with different content for different pages
- remove/hide pages from users backend
- List child pages by slug not ID?
- get_pages() & “child_of”
- Combining ‘depth’ with ‘include’ in wp_list_pages()
- Removing Parent Page URL While Keeping the Navigation Intact
- Organizing Your Pages
- Custom page template for multiple pages
- Can a user find a child page?
- Display child pages in sidebar of parent page in wordpress
- Display child pages in a parent page? [closed]
- Redirect page to default subpage
- How to list a page tree?
- List all posts with the parent or grandparent of a page with a specified ID
- Page access only from a specific page in wordpress website
- Keep page hierarchy in wp_list_pages, even if on a child or grandchild
- Listing all child-pages of a parent-page on the parent-page and each child-page?
- How to control display of page lists on sidebar that doesn’t have childrens
- Check if page has subpages
- Exclude child pages args array
- Search anything from a Child and Sub-Pages of their Parent page
- How to get an array of pages ID by some page’s slug and all its children pages in get_posts() function?
- Return true if parent page id matches
- If page or sub page not working as expected
- How to use wp_dropdown_pages or wp_list_pages to accomplish a menu like this?
- Display grandchild page content on parent page
- Display inner pages like posts.[Like how post excerpt works]
- How to show the view more on my display of pages shortcode
- Childpage title + content on Parent Page
- How to copy specific page content and create child page?
- Pages redirecting to homepage
- Firing page_publish where page is child page
- How to finish this loop?
- displaying child pages in columns when on a parent page
- How to make in post Parent page children page list nummbered
- Woocommerce – Making product pages child of shop
- List of pages – AJAX load more
- Restrict admin access to certain pages for certain users
- WordPress parent select need to be removed
- Limit number of pages that use a specific template?
- Multiple pages on a single page
- Show different badge based upon the user role
- Dashboard–> All Pages “How to create folders?”
- Putting footer content in a “page” – Doing it wrong?
- How to create wordpress page that shows posts with specific tags?
- Share parent path between Custom Post Types and Pages?
- Is there a better way to programmatically insert content into a page?
- Custom page template not working
- How to Add a Rewrite Rule for Only One Page?
- Are Pages the right thing to use for making sections of a single-page site?
- Allow contributors to create page (but not publish)
- add new metabox to page editor with just page parent
- How to setcookie if is_page(‘page’) ? should I use add_action(‘init’) or there is another action?
- I Changed the Menu Order, But the Page Order Didn’t Change on Front Page
- Page template no longer exists – metaboxes won’t save
- A static page view call every custom page templates
- Wrapping images in tags based on size
- Get the ID of the direct parent page
- How to highlight 1st level menu item based on actual page
- How to add prefix slug in static wordpress page?
- access control for assets in a website powered by wordpress
- How to make a page unsearchable in blog search?
- Getting ACF Field in Page – From the Footer
- Rewrite to load homepage for a different url
- logout redirect only from admin page
- Show content if page is a grandchild of top level page
- Static Website No Titles But Still Nav
- Search results to be only posts & children of page ID
- Embedding Password Protected (Private) “Blog Posts” BLOCK that shows Images and Excerpts on Password Protected Page (not the home page)
- On the list of Pages, clicking to Edit a particular page redirects to the list of trash
- My pages are using my Index.php and not my page.php
- WordPress custom pages in a folder
- How to detect if some page template has been selected
- Why Google map can not show full size in a page?
- Page view displays not properly after update wordpress
- What’s the best way to manage sections on a page?
- Display custom_post_type with current taxonmy of a page
- Prevent page from displaying
- Loading custom jQuery and HTML in a WordPress page
- Add feed to a custom page
- Get current post’s child page?
- link a custom page into menu
- Show different number of posts on second page of category
- Use get_posts() with ‘post’ and ‘page’ queries at the same time?
- How can a user login on a wordpress page?
- For some reason my posts are not showing up on my front page [closed]
- How to view front-end of WordPress website, using Atom?