I have been searching for the right solution but I had no luck yet. I am trying to display a list of grandchild pages in a child page, but I just don’t seem to be able to. My parent page is “Fungal species” and its child-pages are two: determined and undetermined (see https://www.neotropicalfungi.com/wp/). I would like to have an alphabetical list on each child page, showing all the names of the mushrooms, each one linking to its own page with pictures and description etc. (like I manually did here: https://www.neotropicalfungi.com/wp/fungal-species/determined/). I already created a page-template and added some php code to functions.php, but this works with child pages only (see https://www.neotropicalfungi.com/wp/provalista/). Can anyone please help? I hope I explained myself clearly.
Thanks in advance
Ciao
Enrico
]]>Could you give us a snippet of your code to see what have you managed to do and try to help out?
It shouldn’t be something totally different than a normal get_page_children() loop.
— Edit —-
I’ve managed to make you a simple template to achieve what you like as far as grabbing even grandchildren and further. You will have to adjust that to your needs though and figure out a way for the Alphabetical index you want. I hope this helps a bit.
<?php
// Template Name: test
get_header();
$pages = get_posts(array(
'post_type' => 'page',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1
));
global $post;
?>
<ul>
<li>
<?php echo $post->post_title; ?>
<?php $children = get_page_children($post->ID, $pages); ?>
<ul>
<?php foreach ($children as $child): ?>
<?php if ($child->post_parent == $post->ID): ?>
<li>
<?php echo $child->post_title; ?>
<?php $granchildren = get_page_children($child->ID, $pages); ?>
<ul>
<?php foreach ($granchildren as $grandchild): ?>
<li><?php echo $grandchild->post_title; ?></li>
<?php endforeach; ?>
</ul>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</li>
</ul>
<?php
get_footer();
?>
If you assign the ‘test’ template to a parent page you’ll see a ul list of all it’s children, grandchildren and so on so forth.
————-
Best regards,
Konstantinos
thanks a lot for your help. I modified a bit the template in order to show the grandchildren only, and now I have to find a way to transform the list to have links pointing to the grandchild pages…. The Alphabetical index will be the next step :)…..
Ciao and thanks again
Enrico
]]>This code will give you the links as well.
<?php foreach ($granchildren as $grandchild): ?>
<li><a href="<?php echo get_permalink($grandchild->ID); ?>"><?php echo $grandchild->post_title; ?></a></li>
<?php endforeach; ?>
Best regards,
Konstantinos
<?php
// Template Name: test
get_header();
$pages = get_posts(array(
'post_type' => 'page',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1
));
global $post;
?>
<ul>
<?php $children = get_page_children($post->ID, $pages); ?>
<ul>
<?php foreach ($children as $child): ?>
<?php if ($child->post_parent == $post->ID): ?>
<li>
<?php echo $child->post_title; ?>
<?php $granchildren = get_page_children($child->ID, $pages); ?>
<ul>
<?php foreach ($granchildren as $grandchild): ?>
<li><a href="<?php echo get_permalink($grandchild->ID); ?>"><?php echo $grandchild->post_title; ?></a></li>
<?php endforeach; ?>
</ul>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</li>
</ul>
thanks again
Best regards
Enrico
]]>Are you getting any errors?
Best regards,
Konstantinos
https://www.neotropicalfungi.com/wp/fungal-species/determined/
Thanks
Best regards
Enrico
]]>For some reason there is no link in your post and I’ll take a look as soon as possible.
Best regards,
Konstantinos
Thanks
Enrico
]]>Please change this:
<?php echo $child->post_title; ?>
to this:
<a href="<?php echo get_permalink($child->ID); ?>"><?php echo $child->post_title; ?></a>
All your ‘children’ and ‘grandchildren’ will now have links.
Best regards,
Konstantinos