Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other WordPress listing subpages

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #31614
    lyleyboy
    Member

    Hi,

    I have a wordpress site which has two levels of navigation. Something like

    Home
    About
    -About 1
    -About 2
    Services
    -Services 1
    -Services 2

    What I’d like is to display the subpages in the sidebar for the page I’m on.
    For example if I’m on the about page or about 1 or about 2 the list would show
    about 1
    about 2

    So I’m not showing subpages but I’m showing the subpages of the section I’m in.

    I hope that makes sense. Thanks in advance.

    #60605
    gno
    Member

    look into the wp_list_pages function.

    something like:

    
    wp_list_pages(array('child_of' => CURRENTPAGEID));
    ?>

    Current page id should be replaced with a variable containing the id of the current page. And to make it fool proof you should check if there is a result before starting the output. (Not outputting directly requires you to set echo => false

    #60607
    davidlab.be
    Participant

    There are many ways to get this done. Alot depends also in how you made the pages…meaning are they custom page templates, do all sub-pages then use the same template. My point is the way you have your site structured changes what conditions you need to check for. This is something I just used yesterday for the same idea using categories. My main navbar is a set of 5 categories that act like containers for the sub-categories. In the sidebar I then only want the sub-pages (children) showing for the selected main category that was selected. Here is the code that is placed in the sidebar:


    $post = $wp_query->post;
    if (in_category('house')) {
    ?>

      ' . __('Around The House') . '') ?>
    }elseif (in_category('food_drink')) {
    ?>

      ' . __('Food and Drink') . '')?>
    }elseif (in_category('garden')) {
    ?>

      ' . __('In the Garden') . '')?>
    }elseif (in_category('beauty')) {
    ?>

      ' . __('Beauty') . '')?>
    }elseif (in_category('shopping')) {
    ?>

      ' . __('Shopping') . '')?>
    }
    else{?>


    }

    ?>

    First is checks the category that it is in. So for the first “if” statement it checks if the category is “house” or a child of “house” by using the in_category() function. Then if it is true it will list the children of the category using the wp_list_categories() function by passing in the “id” of the parent category that you want the sub-pages for. In this case the “id” is 4. Add a title and you are good to go. Each statement is checked till one is true.

    Now…for pages you can use the same idea, but like I said there are so many ways to do this. If your site is not that big then you can use the is_page() function to check the the parent and children pages and then display the children of the parent. So for example:
    The parent page is “Products” and the children pages are “Computers” and “Displays”:
    First check for these pages:


    if (is_page(array('products','computers','displays'))){
    " sub-menu for "products "
    }

    This checks if the page being viewed is any of the 3 pages and if true we will then display the the children pages for the “Products” page like so:


    if (is_page(array('products','computers','displays'))){?>
      wp_list_pages('child_of=55&title_li=

      ' . __('Products') . '

      ')
      ?>

      }
      ?>

    Just pass the “id” of the parent into the “child_of=”. In this sample it was Products with an “id” of 55. Then do the same for every parent page using the elseif as in the first sample using categories above.

    That is the idea anyways…this is not tested so you may need to tweak it. You can also pass the actual “id” into the armament’s if you want.

    Good luck.
    David
    Springlab

    #60597
    leonzinger
    Member

    I have made something very similar on my website, and it goes like this


    $root_parent = get_root_parent($post->ID);
    $parent = $root_parent;
    $args=array(
    'child_of' => $parent
    );
    $pages = get_pages($args);
    if ($pages) {
    $pageids = array();
    foreach ($pages as $page) {
    $pageids[]= $page->ID;
    }
    $args=array(
    'title_li' => "",
    'include' => $parent . ',' . implode(",", $pageids)
    );
    wp_list_pages($args);
    }
Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Other’ is closed to new topics and replies.