dragunoff

[WP] Function: is_tree()

Mar 15th, 2011
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.57 KB | None | 0 0
  1. /**
  2.  * is_tree()
  3.  *
  4.  * @url http://codex.wordpress.org/Conditional_Tags#Testing_for_sub-Pages
  5.  */
  6. if ( !function_exists ( 'is_tree' ) ) {
  7.  
  8.     function is_tree( $pid ) {      // $pid = The ID of the page we're looking for pages underneath
  9.  
  10.         // check only pages
  11.         if (!is_page() )
  12.             return false;
  13.        
  14.         // load details about this page
  15.         global $post;
  16.        
  17.         // if a string is passed, get that page's id by path
  18.         if ( is_string( $pid ) )
  19.             $pid = get_page_by_path( $pid )->ID;
  20.        
  21.         // we're at the page or at a sub page
  22.         if ( is_page( $pid ) )
  23.             return true;
  24.  
  25.         // get all of the page's ancestors
  26.         $anc = get_post_ancestors( $post->ID );
  27.         // loop 'em
  28.         foreach ( $anc as $ancestor ) {
  29.             // untill finding the one that matches
  30.             if( is_page() && $ancestor == $pid ) {
  31.                 return true;
  32.             }
  33.         }
  34.        
  35.         // we arn't at the page, and the page is not an ancestor
  36.         return false;  
  37.        
  38.     }
  39.    
  40. }
  41.  
  42. /**
  43.  * Checks whether current page is a subpage or a top-level page
  44.  *
  45.  * @url http://codex.wordpress.org/Conditional_Tags#Testing_for_sub-Pages
  46.  */
  47. if ( !function_exists ( 'is_subpage' ) ) {
  48.  
  49.     function is_subpage() {
  50.    
  51.         // load details about this page
  52.         global $post;                                
  53.        
  54.         // test to see if the page has a parent
  55.         if ( is_page() && $post->post_parent ) {
  56.        
  57.             // the ID of the parent is this
  58.             $parentID = $post->post_parent;
  59.             // return the ID
  60.             return $parentID;
  61.            
  62.         // there is no parent so...
  63.         } else {
  64.             // ...the answer to the question is false
  65.             return false;                          
  66.         }
  67.        
  68.     }
  69.    
  70. }
Advertisement
Add Comment
Please, Sign In to add comment