Grow your CSS skills. Land your dream job.

WordPress listing subpages

  • # February 14, 2011 at 6:49 am

    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.

    gno
    # February 14, 2011 at 9:07 am

    look into the wp_list_pages function.

    something like:

    < ?php
    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

    # February 14, 2011 at 9:34 am

    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:


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

      < ?php wp_list_categories('orderby=id&show_count=1&child_of=4&title_li=

      ' . __('Around The House') . '') ?>

    < ?php ;
    }elseif (in_category('food_drink')) {
    ?>

      < ?php wp_list_categories('orderby=id&show_count=1&child_of=16&title_li=

      ' . __('Food and Drink') . '')?>

    < ?php ;
    }elseif (in_category('garden')) {
    ?>

      < ?php wp_list_categories('orderby=id&show_count=1&child_of=5&title_li=

      ' . __('In the Garden') . '')?>

    < ?php ;
    }elseif (in_category('beauty')) {
    ?>

      < ?php wp_list_categories('orderby=id&show_count=1&child_of=13&title_li=

      ' . __('Beauty') . '')?>

    < ?php ;
    }elseif (in_category('shopping')) {
    ?>

      < ?php wp_list_categories('orderby=id&show_count=1&child_of=14&title_li=

      ' . __('Shopping') . '')?>

    < ?php ;
    }
    else{?>

      < ?php wp_list_categories()?>
    < ?php
    }

    ?>

    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:


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

      ' . __('Products') . '

      ')
      ?>

      < ?php
      }
      ?>

    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

    # February 14, 2011 at 11:10 am

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


    < ?php
    $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)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".