Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Testing for sub-Pages
- There is no is_subpage() function yet, but you can test this with a little code:
- function wpdocs_is_subpage() {
- // Load details about this page.
- $post = get_post();
- // test to see if the page has a parent
- if ( is_page() && $post->post_parent ) {
- // Return the ID of the parent post.
- return $post->post_parent;
- // There is no parent so ...
- } else {
- // ... The answer to the question is false
- return false;
- }
- }
- You can create your own is_subpage() function using the code in Snippet 2. Add it to your functions.php file. It tests for a parent page in the same way as Snippet 1, but will return the ID of the page parent if there is one, or false if there isnβt.
- /**
- * Check whether we are on a subpage
- *
- * @return mixed ID of the parent post or false if this is not a subpage.
- */
- function wpdocs_is_subpage() {
- // Load details about this page.
- $post = get_post();
- // test to see if the page has a parent
- if ( is_page() && $post->post_parent ) {
- // Return the ID of the parent post.
- return $post->post_parent;
- // There is no parent so ...
- } else {
- // ... The answer to the question is false
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement