Forums

[resolved] Display subpages if parent selected (9 posts)

  1. wp-rory
    Member
    Posted 2 years ago #

    Hi all,

    I was wondering if anybody knows of a way to display subpages in a (widget-made) list of pages only if the parent page is selected?

    For instance, say there were three parent pages - dogs, cats and hamsters, and that hamsters page had two subpages - Russian hamsters and Syrian hamsters, how could I make sure that those two subpages are only shown in the list when the hamsters parent page is selected?

    I am using the WP-Andreas theme and I know it has a subpages menu on the right hand side of the page but I don't want to use that - I want to keep everything in the main left-hand side menu.

    Would it be helpful to give the hamsters page a separate template and do it there using wp_list_pages somehow (perhaps making a depth of 1 there where everything else would be 0?)? My problem there is that the page menu is made using a wp_list_pages line that is in the header.php file, not in any individual page template.

    I have been rifling through forum threads and codex stuff on this topic but I can't seem to make the links to do this.

    Any help greatly appreciated!

    All the best,

    Rory. :)

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    Might consider downloading and installing Otto's PHP Code Widget. Then take the code from the your theme that does the "subpages menu" and use that in one of those php code widgets.

  3. wp-rory
    Member
    Posted 2 years ago #

    Hi Michael,

    Thanks for your speedy reply. I tried what you suggested but unfortunately it just broke the site * and no menus were displayed and neither was any of the site content apart from the header and background image. Furthermore, a strange bug occured where the PHP widget wasn't displayed so I couldn't even remove it! In the end I de-activated the PHP Widget and I'm back to normal now but still no further advanced with my subpages being displayed when the parent is selected.

    Any other ideas on this?

    Thanks,

    Rory. :)

    * The code I put in the PHP widget that caused this trouble was:

    <?php
    function list_subpages_andreas01($return = 0) {
    global $wpdb, $post;
    $current_page = $post->ID;
    while($current_page) {
    $page_query = $wpdb->get_row("SELECT ID, post_title, post_parent FROM $wpdb->posts WHERE ID = '$current_page'");
    $current_page = $page_query->post_parent; }
    $parent_id = $page_query->ID;
    $parent_title = $page_query->post_title;
    if($wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = '$parent_id' and post_type = 'page' AND post_status = 'publish' ")) {
    echo'<div id="subpages"><h2>Hamsters Subpages:</h2> <ul class="submenu">';
    $html = wp_list_pages("child_of=$parent_id&depth=$depth&echo=".(!$return)."&title_li=0&sort_column=menu_order");
    echo'</ul></div>'; }
    if($return) {
    return $html;
    } else {
    echo $html; } }
    ?>
  4. MichaelH
    Volunteer
    Posted 2 years ago #

    hmm, you might only need
    <?php list_subpages_andreas01(); ?>

  5. wp-rory
    Member
    Posted 2 years ago #

    Sounds good but unfortunately I can't even get to the stage of entering that code because the PHP Code widget plugin breaks my site even before I put anything in and also when I go to Dashboard>Appearance>Widgets it tells me I have 1 widget active but it won't display it on the right-hand side where widgets are normally displayed and so I can't remove it. This is still the case even when I deactivate, delete and re-upload and activate the PHP Code Widget.

    Looking at what Firebug tells me about the broken site when the PHP widget plugin is activated, Firefox loads the header image, background image and the wrap div width and then only loads as far as the execphpwidget div - no more of the site is loaded after that.

    Is it possible that the code I entered (pasted in previous post)broke the site and is stored somewhere in the site and so keeps causing trouble every time I activate the PHP widget plugin?

    Rory. :)

  6. wp-rory
    Member
    Posted 2 years ago #

    OK I have managed to put the code you suggested in and get it working...my only question is now how can I make those subpages only appear when the parent page is selected? At the moment those subpages are displayed no matter what page is selected.

    Thanks a lot for any help,

    Rory. :)

  7. MichaelH
    Volunteer
    Posted 2 years ago #

    Not sure if this is what you are wanting but used this before on another thread

    For use in sidebar, display only top level Pages, but when viewing a Page that has children (or is a child) show only children of that parent
    When visiting main page, all top level pages are listed in the sidebar.
    When click on a top level page with no children, all top level pages are listed.
    When click on a top level page with children, just the children pages are listed.
    When click on a children page, just the children of that parent are listed.

    <?php
    $output = wp_list_pages('echo=0&depth=1&title_li=<h2>Top Level Pages </h2>' );
    if (is_page( )) {
      $page = $post->ID;
      if ($post->post_parent) {
        $page = $post->post_parent;
      }
      $children=wp_list_pages( 'echo=0&child_of=' . $page . '&title_li=' );
      if ($children) {
        $output = wp_list_pages ('echo=0&child_of=' . $page . '&title_li=<h2>Child Pages</h2>');
      }
    }
    echo $output;
    ?>

    At least that will give you some ideas of what you can do.

    Also see the template tag, wp_list_pages() article

  8. wp-rory
    Member
    Posted 2 years ago #

    Hi Michael,

    Thanks for your helpful suggestion. In the end I tried a couple of other things.

    Firstly, the wp-flexi-pages plugin widget, which worked really well and achieved what I wanted but unfortunately didn't get on well with my theme.

    So my second way was to define some custom page templates for the pages which had children that I wanted to display, along with custom header files to call from within those templates. The custom header files included the child pages when they loaded the left-hand page menu, whilst the default header didn't. In this manner, I could have a nice menu of only parent pages on the front page, and then when somebody clicked on a page with subpages, they could see that pages subpages only. Just what I wanted.

    The only other thing I had to do was exclude child pages of other parent pages - e.g. if I wanted only sub-pages of the hamster page to show up when the user clicked on hamster, I made sure in that custom header files to exclude the sub-pages of the dogs page. Vice-versa for the dogs page.

    If anybody wants to see the code I used do let me know.

    All the best and thanks again for your help with this,

    Rory. :)

  9. mori-muc
    Member
    Posted 2 years ago #

    Does anybody know how to "link" the "h2"-headline with an anchor?

    <?php
    function list_subpages_andreas01($return = 0) {
    ....
    echo'<div id="subpages"><h2>Hamsters Subpages:</h2> // old

    <h2>Hamsters Subpages:</h2> // new but how create the url?

    THANX !!!!

Topic Closed

This topic has been closed to new replies.

About this Topic