Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to build a menu on child pages that is structured like this

  • Grandparent
    • Parent
      • Current Page
      • child page
      • child page
    • Item 2
    • Item 3
    • Item 4

For one set of parent and child pages everything works like it should.

<ul>
    <li><a href="#"></a></li>
    <li><a href="#"></a></li>
    <li class="page_item_has_children current_page_ancestor current_page_parent">
        <a href="#"></a>
        <ul class="children>
            <li class="current_page_item">
                <a href="#"></a>
            </li>
            <li><a href="#"></a></li>
        </ul>
    </li>
</ul>

However, for another set of parents and children the menu output looks like this:

<ul>
    <li class="current_page_item">
        <a href="#"></a>
    </li>
    <li><a href="#"></a></li>
    <li><a href="#"></a></li>
    <li><a href="#"></a></li>
    <li class="page_item_has_children current_page_ancestor current_page_parent">
        <a href="#"></a>
    </li>
</ul>

This is the relevant code where I'm building the menu.

$ancestors = get_post_ancestors( $post->ID );
$ancestor_id = end($ancestors);
$descendants = get_pages(array('child_of' => $ancestor_id));
$incl = "";

foreach ($descendants as $page) {
    if (($page->post_parent == $ancestor_id) ||
    ($page->post_parent == $post->post_parent) ||
    ($page->post_parent == $post->ID))
    {
        $incl .= $page->ID . ",";
    }
}
$children = wp_list_pages("title_li=&include=".$ancestor_id."&echo=0");
$children.= wp_list_pages("title_li=&child_of=".$ancestor_id."&include="
    . $incl ."&echo=0");

Is there any reason why the same code would generate a different output even though the parents and children are all set up the similarly?

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.