Advertisement
Guest User

Wordpress - List child pages for sub-nav

a guest
Sep 13th, 2013
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.80 KB | None | 0 0
  1. // ECHO SUB-NAV
  2.  
  3. function echo_sub_nav($postID,$forceCurrent=false){
  4.     if($forceCurrent){
  5.         echo '<style type="text/css">#sub_nav ul li.page-item-'.$postID.' a{color: #b30f21;}</style>';
  6.     }
  7.     if(is_subpage($postID)){
  8.         $tempPost = get_post($postID);
  9.         $subPages = wp_list_pages('title_li=&echo=0&child_of='.$tempPost->post_parent);
  10.     }else{
  11.         $subPages = wp_list_pages('title_li=&echo=0&child_of='.$postID);
  12.     }
  13.     if(!empty($subPages)){ ?>
  14.         <nav id="sub_nav">
  15.             <h2><?php if(is_subpage($postID)){
  16.                 echo get_the_title($tempPost->post_parent);
  17.             }else{
  18.                 echo get_the_title($postID);
  19.             }?></h2>
  20.             <ul>
  21.                 <?php echo $subPages; ?>
  22.             </ul>
  23.         </nav>
  24.     <?php }
  25. }
  26.  
  27. // DEFINE SUB-PAGES
  28.  
  29. function is_subpage( $postID = null, $page = null ){
  30.     $post = get_post($postID);
  31.     // does it have a parent?
  32.     if ( ! isset( $post->post_parent ) || $post->post_parent <= 0 )
  33.         return false;
  34.     // is there something to check against?
  35.     if ( ! isset( $page ) ) {
  36.         // yup this is a sub-page
  37.         return true;
  38.     } else {
  39.         // if $page is an integer then its a simple check
  40.         if ( is_int( $page ) ) {
  41.             // check
  42.             if ( $post->post_parent == $page )
  43.                 return true;
  44.         } else if ( is_string( $page ) ) {
  45.             // get ancestors
  46.             $parent = get_ancestors( $post->ID, 'page' );
  47.             // does it have ancestors?
  48.             if ( empty( $parent ) )
  49.                 return false;
  50.             // get the first ancestor
  51.             $parent = get_post( $parent[0] );
  52.             // compare the post_name
  53.             if ( $parent->post_name == $page )
  54.                 return true;
  55.         }
  56.         return false;
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement