• I am currently in the process of migrating a static site into wordpress. The site uses a main horizontal nav, and a sub menu on the left side which is dependant upon the parent navigation.

    I have created all the pages in WordPress, and limited the top nav to parent pages using wp_list_pages(‘title_li=&depth=1’). Great, so that works well.

    Then I try to have the sub pages for the selected parent appear on the left using wp_list_pages(‘title_li=’ . the_title() . ‘&child_of=<page ID>’). All that does is list the currently selected page. I have tried a few other WordPress codes but I am not getting what I want.

    I have searched these forums and the codex extensively but had no luck finding a solution.

    My request is not too dissimilar to this one here: https://www.ads-software.com/support/topic/23638

    So can this be done dynamically, or do I have to have a new template for every page?

    Regards,
    JIMbob

Viewing 15 replies - 1 through 15 (of 15 total)
  • Depending on how many Page Parents you have, you could use conditional tags
    https://codex.www.ads-software.com/Conditional_Tags
    So <?php
    if (is_page(firstparentID)) {
    wp_list_pages('title_li=' . the_title() . '&child_of=firstparentID') }
    elseif (is_page(secondparentID)) {
    wp_list_pages('title_li=' . the_title() . '&child_of=secondparentID') }
    ?>

    etc.

    Thread Starter jimbo

    (@jimbo)

    Thanks for your reply, but I am a complete php hack. I understand that code, but for some reason it throws an error:
    Parse error: parse error, unexpected ‘}’ which is just after &child_of=firstparentID bit.

    I have six parent pages and within those parents and they have anything between 0 and 3 sub pages.

    Sorry, like I said, I’m learning this on the fly as well. I think I left out a semicolon after each wp_list_pages should be like thiswp_list_pages('title_li=' . the_title() . '&child_of=secondparentID'); }Hopefully someone more skilled will chime in.
    And just so we are on the same page, you are putting the actual ID numbers in the code. With 6 parent pages, you’ll have 6 conditionals in the sidebar.

    Thread Starter jimbo

    (@jimbo)

    Ok, so it’s working when I put an ID in. eg.
    <?php wp_list_pages(‘title_li=’ . the_title() . ‘&child_of=9’); ?>

    So how do I do it so that it works dynamically? Currently the sub pages of parent id 9 are showing in the sidebar for all pages, which is to be expected as I hard coded the id of 9 in.

    Cheers.

    That is where the conditionals come into play.
    <?php
    if (is_page(8)) {
    wp_list_pages('title_li=' . the_title() . '&child_of=8') }
    elseif (is_page(9)) {
    wp_list_pages('title_li=' . the_title() . '&child_of=9') }
    ?>

    etc.
    Where as the 8s and 9s would be the parent pages.You simply would do that for the 6 parent pages.
    I’m really hopping someone comes along and has a shortcut than having to have all 6, but the get I could find a tag that would allow to get a page’s ID. If something like that exsisted, there would be a way of using a $ID=some_tag()
    and then echoing the $ID into the wp_list_pages.
    But again, I’m still stumbling around the dark on some of the advanced features.

    Thread Starter jimbo

    (@jimbo)

    Thank you, miklb. This works perfectly well. It’s not completely dynamic, but it’s the solution I am looking for. Thanks again.

    My pleasure, and I learned a little more along the way myself. Perhaps in future versions of WP there will be a tag that allows to retrieve the page ID, thereby simplifying the process.

    Normally what I would do in this case is create my own function, add this to a file say theme_functions.php and include this in header.php. You’d be surprised after looking at the tables how much easier it is just to make you own queries to get the info you need ??

    Thread Starter jimbo

    (@jimbo)

    Ok, I’ve run into a bit of a problem with the above code. When I click onto one of the subpages, the subpage menu disappears from the sidebar. Is this an expected behaviour?

    I suppose it is. I assumed since you said you had a horizontal menu with the parent pages, you were mainly concerned with showing the children of that page in the sidebar. My bad. Perhaps someone will come along and sort this out. Sorry for leading you down a dead end road, so to speak.

    Thread Starter jimbo

    (@jimbo)

    anyone know of a fix?

    $page_id worked for me when I was trying to do a similar thing.

    I’ve found a solution for small sites that need this.

    In each page in your menu structure add a ‘custom field’ (at the bottom of the manage post page in the admin area).

    Find out the ID of the main section page. For instance my About page has an ID of 2 – this appears to the left of the page name on the Manage Pages page.

    Add a custom field with the name ‘section’ and the value 2 to each page within that section of your site.

    Then, to make your menu, edit your header.php page and paste in the following code.

    <?php
    if($section_id = get_post_meta($page_id, ‘section’, $single = true))
    {
    wp_list_pages(‘depth=1&sort_column=menu_order&title_li=&child_of=’.$section_id );
    }
    ?>

    Instant submenu that does not disappear when you click on one of the pages in the section.

    I was struggling with this same issue, but it occurred to me that you could do it with these CSS rules:


    .page_item ul li {
    display: none;
    }
    .current_page_item ul li, .page_item .current_page_item {
    display: block;
    }

    Hope that helps.

    Is there a way to only show the page that you are in?

    so if I am in https://www.blog.com/page1

    I won’t see page 2?

    This needs to be dynamic. i don’t want to have to hard code it. Because pages will be added and others deleted “randomly”.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Dyanmic sup page links on parent’ is closed to new replies.