- This topic is empty.
-
AuthorPosts
-
January 16, 2012 at 8:25 am #36139hblackettMember
Hi,
I’m trying to modify the following piece of code:$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
if ($children); { ?>
Currently I’m using the above to output a list of child pages on my website. That’s working fine.
However, I want to add the conditional statement that the list should only appear if there is more than one child page.
i.e. If there is only one child page there should be no list.Thanks, any advice would be very gratefully received!
January 16, 2012 at 12:06 pm #94736TheDocMemberA very interesting request!
Let me see here… This is a mashup of some things I’ve found, not sure if it’ll work but hopefully it sends you in the right direction:
- ID) ?>">post_title ?>
$count = 0;
$pages = get_pages('child_of='.$post->ID.'&sort_column=post_modified&sort_order=desc');
foreach($pages as $page) {
$count++;
if ( $count > 1 ) { ?>
}
?>
January 16, 2012 at 2:02 pm #94749hblackettMemberThanks for the reply, Doc, glad it caught your interest!
With your code it does solve the problem of not displaying a list when there is only one child page. However, when there are two or more child pages it doesn’t output the first child page in the list, but continues to output the rest of the list as normal. Curious!
I’m going to look into things and try for a workaround, but if anyone does find a solution in the meantime that would be enormous help.
January 16, 2012 at 2:36 pm #94758TheDocMemberAaahhh yes… Well then maybe you could just do this:
$count = 0;
$pages = get_pages('child_of='.$post->ID.'&sort_column=post_modified&sort_order=desc');
foreach($pages as $page) {
$count++;
if ( $count = 2 ) {
wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
}
}
?>
Again, not my realm of expertise, but my thinking here is “if the count gets to 2, then display child pages.
January 17, 2012 at 6:15 pm #94882hblackettMemberHmm, have just tried the above and it doesn’t display anything. Thanks for your help though & pointing me in the direction of $count (I hadn’t used that before), it’s much appreciated. I’m also looking at this thread and will surely find a solution soon. Will post back when I do!
January 19, 2012 at 4:19 pm #95013hblackettMemberI’ve found a working solution so thought I’d share. Not sure if it could be streamlined at all, but it does the job for me so I’m happy to sign off on this, hope it’s of use for others.
$include_page_ids = array( $post->ID );
for ($i = 0;$i < count($include_page_ids);$i++) {
$children = get_pages('child_of='.$include_page_ids[$i]);
if (count($children) > 1) {
$sub = "- ";
- → ";
$sub .= $child->post_title;
$sub .= " ";
foreach ($children as $child){
$sub .= "
}
$sub .="
echo $sub;
}
}
?> -
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.