How to list the current page’s children?
-
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?
-
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
adsworthAdi:
Thanks. I have taken care of it.
Appreciate all of your help..
–AlexAybi1, could you post your final code so we can all learn from what you figured out. This is interesting.
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.
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 https://www.adsworth.info/wp-pagesnavRegards
Adi@ 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)
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 4I 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.
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.
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).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
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:
https://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: https://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”…..
https://thebeadedrose.com/?page_id=14Problem #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,
AmandaTrying 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?
kbiglione, send an email to me at susan [at] auntialias [dot] com and I’ll send you what I’ve got.
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.
I too needed a variant of wp_list_pages() that expanded the current page’s brother/sister pages, but collapsed parent, grandparent (etc.) and other pages in the sidebar.
Adsworth’s plugin was almost (but not quite) what I was looking for and (despite my best efforts) I was unable to modify it to create what I was after.
However, I did yet more searching and I’ve now found the solution:
It’s a plugin called ‘Fold Page List’ and is written by WebSpace Works – download it here:
https://www.webspaceworks.com/resources/cat/wp-plugins/30/
Regards,
Greg
- The topic ‘How to list the current page’s children?’ is closed to new replies.