0

So I don't really know why I am having so much trouble with this, it seems like functionality in wordpress that should be a bit easier to working with.

Anyhow I need to generate a menu system for a sidebar that looks like this (for a top level page):

<ul>
    <li><a>Sub-Page</a></li>
    <li><a>Sub-Page</a>
        <ul>
            <li><a>Sub-Sub-Page</a></li>
            <li><a>Sub-Sub-Page</a></li>
        </ul>
    </li>
    <li><a>Sub-Page</a>
        <ul>
            <li><a>Sub-Sub-Page</a></li>
            <li><a>Sub-Sub-Page</a></li>
            <li><a>Sub-Sub-Page</a></li>
        </ul>
    </li>
    <li><a>Sub-Page</a></li>
</ul>

On top if this, I need to show the same menu, no matter how deep in the sub pages the user is at.

For example, if I was on a sub-sub-page, then I still wish to show the same menu.

In other words, even if I was at a 3rd level page, it would still show 3rd and 2nd level pages - exactly the same as the above menu.


Thanks for the help in this one!

2
  • What is the code that creates the menu?
    – s_ha_dum
    Commented Jan 11, 2013 at 1:36
  • I don't really have code that 'creates' the menu besides what I have answered with :P
    – Ben Potter
    Commented Jan 14, 2013 at 1:12

1 Answer 1

1

So it turns out to be a bit easier than I thought:

                <?php

                if ($post->post_parent) {
                    $ancestors=get_post_ancestors($post->ID);
                    $root=count($ancestors)-1;
                    $parent = $ancestors[$root];
                } else {
                    $parent = $post->ID;
                }

                ?>

                <?php $args = array(
                    'depth'        => 0,
                    'date_format'  => get_option('date_format'),
                    'child_of'     => $parent,
                    'title_li'     => __(''),
                    'sort_column'  => 'menu_order, post_title'
                ); ?>


                <ul>

                  <?php wp_list_pages( $args ); ?> 

                </ul>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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