WordPress.org

Ready to get started?Download WordPress

Forums

How to list the current page's children? (39 posts)

  1. ayb1
    Member
    Posted 9 years ago #

    Many of the pages on my site will have child pages attached to them (e.g. "About Us" will have a "Contact Us" child page). Right now, I can have wp_list_pages() just list the parent pages (ie. level=1 option)-- and if I use level=0, it will display all of the child pages (which I don't want as many of my pages will have child pages).

    Is there a way to figure out if the page that is selected/active has "child pages", and, if so, list out the child pages using wp_list_pages() or any other function?

  2. adsworth
    Member
    Posted 9 years ago #

    Hi,

    wp_list_pages has a child_of parameter.
    So calling it like this should work:
    $page_id = the_ID();
    wp_list_pages("child_of=$page_id");

    Regards
    Adi

  3. ayb1
    Member
    Posted 9 years ago #

    That results in:

    WordPress database error: [Unknown column '$page_id' in 'where clause']
    SELECT ID, post_title,post_parent ,UNIX_TIMESTAMP(post_modified) AS time_modified,UNIX_TIMESTAMP(post_date) AS time_created FROM service_posts WHERE post_status = 'static' AND post_parent=$page_id ORDER BY post_title ASC

  4. adsworth
    Member
    Posted 9 years ago #

    Hi,

    then rewrite try this:
    $page_id = the_ID();
    wp_list_pages("child_of=" . $page_id);

    regards
    adi

  5. ayb1
    Member
    Posted 9 years ago #

    Sorry Adi, but now I'm getting:
    Warning: Invalid argument supplied for foreach() in /homepages/35/d93996604/htdocs/service/wp-includes/template-functions-post.php on line 357

  6. ayb1
    Member
    Posted 9 years ago #

    Also, is there a function like has_children() or something that will return a "true" if my page has children?

    Something that I could use to do:

    if (has_children()) {
    [html/css code]
    [php code for wp_list_pages for child_of current page]
    }

  7. ayb1
    Member
    Posted 9 years ago #

    Does the function the_id() work for pages the same as posts?

    The text of the codex for the_id() makes it seem like it is more for posts.

  8. adsworth
    Member
    Posted 9 years ago #

    Hi,

    OK, I know what's happening....
    The call to the_ID() only works after the call to the_post().
    In other words you can only use the_ID() after you have loaded the page/post content with the_post().

    I hope that info helps.

    Regards
    Adi

  9. adsworth
    Member
    Posted 9 years ago #

    If you can't call the_post() earlier you might also try this:

    $lp_param = "";
    if($wp_query->is_page) {
    $lp_param = "child_of=" . $wp_query->get_queried_object_id();
    }
    wp_list_pages($lp_param);

    this should show children of the current page and all pages otherwise.

    there isn't a has_children functionality as far as I know.

    Regards
    Adi

  10. ayb1
    Member
    Posted 9 years ago #

    I appreciate your help, but its just not doing what I want.

    Right now my main horizontal menu lists "Home" and all of my other main parent pages.

    What I'm trying to do is:
    (1) If the user is not on the HOME page, determine if the page has child pages
    (2) create another bar below it so the user can choose from the child pages

    I thought there would be an easy way to do this that would allow any page to have as many "descendant" levels as possible, but there doesn't seem to be an easy way.

  11. ayb1
    Member
    Posted 9 years ago #

    Does anyone have any ideas for this?

  12. adsworth
    Member
    Posted 9 years ago #

    Hi,

    add this just after the main horizontal menu:
    if($wp_query->is_page && !is_home()) {
    $lp_param = "";
    $lp_param = "depth=1&child_of=" .
    $wp_query->get_queried_object_id();
    wp_list_pages($lp_param);
    }

    it will only display the direct childern of the page you are currently viewing.
    What won't work with this, is a third bar if you are viewing a grandchild of a main parent page.

    That's the best I can do, off the top of my head.

    The only other idea I have is call get_pages() which returns
    all pages titles plus ID and parent ID as an array and then
    build the nav yourself.

    Regards
    adsworth

  13. ayb1
    Member
    Posted 9 years ago #

    Adi:
    Thank you! I appreciate your help.. now I am 99% of the way there! The only one thing I need to do is ensure the submenu displays once a child page has been selected.. to display the other child pages..
    --Alex

  14. ayb1
    Member
    Posted 9 years ago #

    It seems like the only way I will get this flawlessly working is to just scrap everything and write my own version of wp_list_pages. Although things work so far, when a child page is selected the menu reloads and reflects as if nothing were selected.

    Does anyone have any thoughts? Has anyone written their own wp_list_pages type function?

  15. adsworth
    Member
    Posted 9 years ago #

    Hi ayb1,

    this is the best I can do with standard WP functions
    http://stegen.dyndns.info:666/apps/wordpress/

    see the box labeled "for ayb1" it is empty when on the homepage
    but as soon as you navigate to one of the pages it'll show the direct subpages, of that page. But not the parent pages aswell.
    This is the code, I added to sidebar.php:
    <?php
    if($wp_query->is_page && !is_home()) {
    $lp_param = "";
    $lp_param = "title_li=&depth=1&child_of=" .
    $wp_query->get_queried_object_id();
    wp_list_pages($lp_param);
    }
    ?>

    Regards
    Adi

  16. ayb1
    Member
    Posted 9 years ago #

    Adi:
    I appreciate your help. I wasn't able to load your web site, but I got the same code working on mine. Thanks.

    The problem is that once I select a child page, the list of other child pages goes away.

    I think there needs to be one more segment of code:

    if (($wp_query->is_page && !is_home()) && (the page is a child page)) {
    1. determine the parent page
    2. display the parent pages, highlighting the parent of the current page
    3. display the row of child pages, highlighting the one that is selected
    }

  17. adsworth
    Member
    Posted 9 years ago #

    Hi ayb1,

    The page is on my laptop and it went into suspend mode over night.

    That with the children is true. For that you would need to roll your own wp_list_pages or whatever you want to call it. The function get_pages() will give you all the pages in the DB, no need to do your own selects. It'll return an array, each entry in the array is an object with the following properties. ID, post_title, time_modified, time_created and post_parent.
    So you should have all the info to build your own nav.
    I'd have a go but I'm totally under with work till mid next week.

    Regards
    adsworth

  18. ayb1
    Member
    Posted 9 years ago #

    Adi:
    Thanks. I have taken care of it.
    Appreciate all of your help..
    --Alex

  19. Lorelle
    Member
    Posted 9 years ago #

    Aybi1, could you post your final code so we can all learn from what you figured out. This is interesting.

  20. jgpippin
    Member
    Posted 9 years ago #

    I second that. I really need that code to get my website working, and I'd love you if you'd come post it for us.

  21. adsworth
    Member
    Posted 9 years ago #

    Hi,

    since I had some time on hand and some people wanted this I went ahead and implemented my understanding of this problem as a plugin for WordPress 1.5.
    Get it here http://www.adsworth.info/wp-pagesnav

    Regards
    Adi

  22. AuntiAlias
    Member
    Posted 9 years ago #

    @ Adi-- Very cool that you did this via plugin! I'll be checking it out. It seems to answer my question (as yet unexpressed in the forum) as well as for this discussion thread. (after I check out the plugin I'll post something to that effect there)

  23. AuntiAlias
    Member
    Posted 9 years ago #

    I'm looking at it, and what I'd like to do is nest the child/siblings list inside the parent list item. Similar to what happens in the wp_list_pages() but only showing the selected children/siblings as appropriate.

    root/parent list item 1
    root/parent list item 2
    --child of parent 2 list item a
    --child of parent 2 list item b
    --child of parent 2 list item c
    root/parent list item 3
    root/parent list item 4

    I see that your code generates separate lists. So that child of parent 2 is a completely separate list that occurs *after* the root/parent list ends. (I am a severe novice at php, but may get help with it to generate nested lists)

    Maybe this would be a helpful example: A sample of html code *and* CSS for horizontal list w/ displayed horizontal sublist, check out the ever-helpful Listamatic site.

  24. AuntiAlias
    Member
    Posted 9 years ago #

    Okay, I've sort of got it now, thanks to my boyfriend who knows C but not PHP, except for what he's picked up helping me learn the most basic basic stuff.

    It's a kind of hybrid between the standard wp_list_pages and adsworth's wp-pagesnav plugin:

    Show top level pages
    show all parents
    show siblings, show self
    show children.

    In a single nested set of lists. (no cousins, tho. that's how it differs from the wp_list_pages)

    The script works, but there's still a bit of cleanup to do. Plus I'm clueless about releasing software as a plugin (and frankly, I don't want to become more proficient in that area, since I think I need to focus in other areas and not become more than an ultra-novice php programmer). So I sent it to adsworth... I hope that it can find a way to get out there, since it meets a slightly different need (nested lists rather than completely separate lists) and I've seen a few threads about this very need on the boards here and there.

  25. tr909
    Member
    Posted 9 years ago #

    It looks like template-functions-post.php (wordpress 1.5) might have a little bug (or two) because first if a page has no childs i get a WARNING instead of just no list at all. Another bug (i think) is: line 351 it says:
    // Output of the pages starting with child_of as the root ID.
    well it only lists the children of that particular ID not that page itself.
    i made a workaround by first getting the parent id (if present)
    $currentparent = ($post->post_parent==0? $page_id : $post->post_parent);
    (in english: if post_parent == 0 then this is a parent itself) after that:
    `$x = get_pages();
    echo $x[$currentparent]->post_title;
    foreach($x as $item) if($item->post_parent=$currentparent) echo $item->post_title;'

    Although wordpress is on the way, the Pages and CMS stuff has to mature a bit.
    I (and others i guess) need a little bit more control over this (preferably thru the
    admin panels).

  26. tr909
    Member
    Posted 9 years ago #

    It looks like template-functions-post.php (wordpress 1.5) might have a little bug (or two) because first if a page has no childs i get a WARNING instead of just no list at all. Another bug (i think) is: line 351 it says:
    // Output of the pages starting with
    child_of as the root ID.
    well it only lists the children of that particular ID not that page itself. i made a workaround by first getting the parent id (if present)
    $pp=($post->post_parent==0?$page_id:$post->post_parent);
    (in english: if post_parent == 0 then this is a parent itself) after that:
    $x = get_pages();
    echo $x[$pp]->post_title;
    foreach($x as $item)
    if($item->post_parent=$currentparent)
    echo $item->post_title;

    Although wordpress is on the way, the Pages and CMS stuff has to mature a bit. I (and others i guess) need a little bit more control over this (preferably thru the admin panels). thnx worpress team for a great product so far

  27. capturinglight
    Member
    Posted 9 years ago #

    Wow. This one is driving me batty! I've spent two long nights on this so far (because i'm stubborn and like to figure things out on my own)... but I give up... so, I'm asking for specific help here. I have tried everything mentioned on this forum in regards to "wp_list_pages" and "sub-navs" and "child_of", etc. I've also tried out the wonderful plugin that Adi listed above, in addition to this one:
    http://www.darkmuse.co.uk/wordpress/customize/plugins/

    I still haven't found a solid solution for what I need my site to do. This is a site I created for a friend of mine, it's located here temporarily: http://thebeadedrose.com.

    All pages on the site are individual "Pages" and "Sub-Pages."

    I was able to get the main nav working finally by using this bit of code in my header:

    <?php wp_list_pages('depth=1&sort_column=menu_order&title_li='); ?>

    So far, so good.

    The problem comes in with my sub-pages. The only ones I have setup now on the test site are under "Company".....
    http://thebeadedrose.com/?page_id=14

    Problem #1 -
    If you click on either of the 2 sub-pages, the sub-nav disappears, the warning appears, and also the "hit state" in the main nav goes away.

    Problem #2 -
    If you click around the rest of the site... you'll see the warning is displayed on every page that has no "children" or subpages.

    This is the code I'm using in the sidebar to display a Page's children:

    <?php wp_list_pages('child_of=' . $page_id . '&title_li=<h3>' . __('Pages') . '</h3>' ); ?>

    I thought about doing an "if/else" statement in the sidebar... something that says "If Page has no child... end.... else, display the children"... but it also needs to say "If Page IS a child itself, display the siblings."

    I love WordPress, but I'm still trying to wrap my head around PHP.

    Thanks for any clues,
    Amanda

  28. kbiglione
    Member
    Posted 9 years ago #

    Trying not to reinvent the wheel here. Adsworth's plugin is nice but my needs are a bit closer to AuntiAlias'. Adsworth, if you're still following this thread, do you plan on releasing an update with the AuntiAlias mods included?

  29. AuntiAlias
    Member
    Posted 9 years ago #

    kbiglione, send an email to me at susan [at] auntialias [dot] com and I'll send you what I've got.

  30. gpalin
    Member
    Posted 9 years ago #

    That's a great plugin, adi. I was trying to figure out how to display a list of child pages for the current page, and the plugin does the trick. I just use the wp_list_pages('title_li=&depth=1&exclude=14&sort_column=menu_order'); function to get the top level pages, since I want to have them appear separately from the cild pages, if any. But the plugin easily handles the listing of any child pages that there may be for the current page.

    I'm new to WP, but familiar with PHP. I'm going to pick apart your plugin and figure out how to make it work, and how I might have solved the problem by using WP's built in functions.

Topic Closed

This topic has been closed to new replies.

About this Topic