WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Listing Sub Pages in Sidebar (11 posts)

  1. robhav
    Member
    Posted 6 years ago #

    Hi

    Does anyone know a way I can display all of my subpages in the sidebar of my site, irrespective of whether you are currently on the parent page.

    At the moment, I am using the following code - but this only shows the subpages when you are on the parent page. I want to show all subpages in one list, throughout the site.

    <?php
    if($post->post_parent)
    $children = wp_list_pages("sort_column=menu_order&title_li=&child_of=".$post->post_parent."&echo=0"); else
    $children = wp_list_pages("sort_column=menu_order&title_li=&child_of=".$post->ID."&echo=0");
    if ($children) { ?>
    <ul>
    <?php echo $children; ?>
    </ul>
    <?php } ?>
  2. MichaelH
    Member
    Posted 6 years ago #

    Didn't test this, but using the code listed in wpdb_Class, get_results section could make your query string be something like:

    $querystr ="
      SELECT $wpdb->posts.* FROM $wpdb->posts
      WHERE $wpdb->posts.post_status = 'publish'
      AND $wpdb->posts.post_type = 'page'
      AND $wpdb->posts.post_parent > '0'
      ORDER BY $wpdb->posts.post_date DESC";
  3. robhav
    Member
    Posted 6 years ago #

    Hi Michael. Thanks for your reply. I'm a newbie so not much of this makes any sense. How do I implement this?

  4. MichaelH
    Member
    Posted 6 years ago #

    You'd put the code in your sidebar.php.

    In addition to the link above, please review

    Editing Files
    Stepping into Templates
    Template Hierarchy

  5. robhav
    Member
    Posted 6 years ago #

    Still a bit confused. I'm well versed in html and css, but don't understand how this is put in the sidebar.php. In it's current format it doesn't do anything.

  6. MichaelH
    Member
    Posted 6 years ago #

    Well let's take a look at what you are doing...please paste all the the code from your sidebar.php at http://wordpress.pastebin.ca/ and report the link back here.

  7. robhav
    Member
    Posted 6 years ago #

    Thanks for your help. I have pasted the sidebar code into http://wordpress.pastebin.ca/812033.

  8. MichaelH
    Member
    Posted 6 years ago #

    Are you using Widgets? Visit your Administration > Presentation > Themes panel to determine that.

  9. MichaelH
    Member
    Posted 6 years ago #

    Assuming you ARE NOT using widgets, then I just replaced your code.

    http://wordpress.pastebin.ca/812044

  10. robhav
    Member
    Posted 6 years ago #

    Thanks for you help. This works great.

    Just one more question - if I wanted to order the subpages using the page order rather than the date - would I change:

    ORDER BY $wpdb->posts.post_date DESC";

    to

    ORDER BY $wpdb->posts.post_title DESC";

  11. robhav
    Member
    Posted 6 years ago #

    Ok I've resolved it - the code is:

    ORDER BY $wpdb->posts.menu_order ASC";

Topic Closed

This topic has been closed to new replies.

About this Topic