0

My site is organised into three sections, like this:

  • Reiki Basics (page ID 499)

    – page 1.1

    – page 1.2

    – page 1.3

  • Practice & Study (page ID 501)

    – page 2.1

    – etc

  • Courses (page ID 471)

    – page 3.1

    – etc.

These all display in the primary menu at the top of the page. When you hover over ‘Reiki Basics’, you see an accordian come down with pages 1.1, 1.2 and 1.3.

I want to create a secondary menu, in the sidebar, that only shows sibling pages (and the parent page).

For example, when the user is browsing any page in the section 'Reiki Basics', including the 'Reiki basics' page itself, they would see links to all the pages in this section (1.1, 1.2, 1.3). They wouldn’t see pages from other sections. The same would work when they are browsing pages in section two.

I want this menu to be created automatically for all new pages. I would rather not install any new plugins, although I do already use 'widget logic', which might help.

2
  • Show what you have done so far, research and code please. Dec 15, 2014 at 15:36
  • @BradDalton I posted this question to give a 'how to' to other people. I answered it myself immediately after posting it. For some reason it got a downvote. I just thought the code might help someone else. Dec 16, 2014 at 6:32

1 Answer 1

-1

The first stage is to create a separate menu for each section of the site. Create three menus called 'Reiki Basics', 'Practice & Study' and 'Courses'. Add all of the relevant pages to each menu.

To make these menus appear in the sidebar, add them using three separate 'Custom menu' widgets. If you check your site now, you will find that all of the menus appear on every page. You're half way there.

To ensure that only the appropriate menus appear on the appropriate pages, you can use widget logic. You need to create a test to see if your custom menus should display or not. For example, you only want the first menu to appear if you are on any of the pages 'Reiki Basics', 1.1, 1.2, or 1.3.

There is no function built in to wordpress (that I am aware of) that will test this easily. The following snippet (from the wordpress codex) however will allow you to do this easily, giving you a new function, 'is_tree'.

function is_tree( $pid ) {      // $pid = The ID of the page we're looking for pages underneath
global $post;               // load details about this page

if ( is_page($pid) )
    return true;            // we're at the page or at a sub page

$anc = get_post_ancestors( $post->ID );
foreach ( $anc as $ancestor ) {
    if( is_page() && $ancestor == $pid ) {
        return true;
    }
}

return false;  // we arn't at the page, and the page is not an ancestor
}

All you need to do now is to use the function is_tree( 'id' ) to see if the current page is the page, or is a sub page of the appropriate page. So for the first section, your test will look like this: is_tree( '499' ).

Here is a screenshot to show how it works:

A screenshot to show how to use widget logic to add custom menus depending on the parent page

Your Answer

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

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