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

Viewing 15 replies - 1 through 15 (of 38 total)
  • 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

    Thread Starter ayb1

    (@ayb1)

    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

    Hi,

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

    regards
    adi

    Thread Starter ayb1

    (@ayb1)

    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

    Thread Starter ayb1

    (@ayb1)

    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]
    }

    Thread Starter ayb1

    (@ayb1)

    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.

    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

    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

    Thread Starter ayb1

    (@ayb1)

    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.

    Thread Starter ayb1

    (@ayb1)

    Does anyone have any ideas for this?

    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

    Thread Starter ayb1

    (@ayb1)

    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

    Thread Starter ayb1

    (@ayb1)

    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?

    Hi ayb1,

    this is the best I can do with standard WP functions
    https://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

    Thread Starter ayb1

    (@ayb1)

    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
    }

Viewing 15 replies - 1 through 15 (of 38 total)
  • The topic ‘How to list the current page’s children?’ is closed to new replies.