Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2017
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. Testing for sub-Pages
  2.  
  3. There is no is_subpage() function yet, but you can test this with a little code:
  4.  
  5. function wpdocs_is_subpage() {
  6. // Load details about this page.
  7. $post = get_post();
  8.  
  9. // test to see if the page has a parent
  10. if ( is_page() && $post->post_parent ) {
  11. // Return the ID of the parent post.
  12. return $post->post_parent;
  13. // There is no parent so ...
  14. } else {
  15. // ... The answer to the question is false
  16. return false;
  17. }
  18. }
  19.  
  20. 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.
  21.  
  22. /**
  23. * Check whether we are on a subpage
  24. *
  25. * @return mixed ID of the parent post or false if this is not a subpage.
  26. */
  27. function wpdocs_is_subpage() {
  28. // Load details about this page.
  29. $post = get_post();
  30.  
  31. // test to see if the page has a parent
  32. if ( is_page() && $post->post_parent ) {
  33. // Return the ID of the parent post.
  34. return $post->post_parent;
  35. // There is no parent so ...
  36. } else {
  37. // ... The answer to the question is false
  38. return false;
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement