0

I'm try to build a menu with wp_list_pages() that must work like this:

Main Page  
  -Subpage1  
  -Subpage2  

I check if the Page has_subpage and i can see the 2 subpages when i'm in the Parent but when i'm visit one of the subpages i don't see anything. So, what i can do to check if page has subpages to get the same list of pages in the subpage(s)?

How i use it: in the functions i check if the Page has_subpage,

function has_subpage (){
global $post;
$pages = get_pages("sort_column=menu_order&depth=1&child_of={$post->ID}");
if ($pages) return TRUE;
}

and in my template i call the menu (list of subpages),

<?php if (is_page() && has_subpage()) { ?>

    <?php
      if($post->post_parent) {
      $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0$sort_column=menu_order");
      $title_heading = get_the_title($post->post_parent);
      }

      else {
      $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
      $title_heading = get_the_title($post->ID);
      }
      if ($children) { ?>

      <?php echo $children; ?>

    <?php } ?>

<?php } ?>

the problem is inside has_subpage() function, cause if i don't use it i get the pages correct, maybe i'm missing something here?

thanks a lot!

1

You have to pass the parent page id to the wp_list_pages function instead of the global $post->ID in your subpages.

function wpse33151_getSubpages() {      
    global $post;

    $parents = get_post_ancestors($post->post_id);
    krsort($parents);
    $parents = array_merge(array(), $parents);

    if (is_home() || is_single()) {
        $id = get_option('page_for_posts');
        $parent = get_post_ancestors($id);
        $id = $parent[0];
    } elseif($parents) {
        $id = $parents[0];
    } else {
        $id = $post->ID;
    }

    $children = wp_list_pages('title_li=&child_of=' . $id . '&echo=0');
    $out = null;

    if ($children) {
        $out = '<div id="subpages" class="widget-container">';
        $out .= '<h3 class="widget-title"><a href="' . get_permalink($id) . '">' . get_the_title($id) . '</a></h3>';
        $out .= '<ul>';
        $out .= $children;
        $out .= '</ul>';
        $out .= '</div>';
    }

    return $out;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.