• Resolved enricolino

    (@enricolino)


    Hello there

    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

    • This topic was modified 7 years, 8 months ago by enricolino.
Viewing 15 replies - 1 through 15 (of 20 total)
  • Hello,

    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

    • This reply was modified 7 years, 8 months ago by Konstantinos Xenos. Reason: adding code
    Thread Starter enricolino

    (@enricolino)

    Hello 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

    Hello again,

    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

    Thread Starter enricolino

    (@enricolino)

    I must have done a mistake somewhere…. Isn’t this what you mean?

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

    Yes, the get_permalink() function returns you the link of any given ID. I just copy paste what you gave me and it works on my site at least as well.

    Are you getting any errors?

    Best regards,
    Konstantinos

    Thread Starter enricolino

    (@enricolino)

    No errors but no links showing, somehow….. Please take a look here:

    https://www.neotropicalfungi.com/wp/fungal-species/determined/
    Thanks

    Best regards

    Enrico

    • This reply was modified 7 years, 8 months ago by enricolino.

    Please edit your post and paste your link again for the front-end test page.

    For some reason there is no link in your post and I’ll take a look as soon as possible.

    Best regards,
    Konstantinos

    Thread Starter enricolino

    (@enricolino)

    Sorry, I didn’t realize… Post edited…..

    Thanks

    Enrico

    I think I know what’s up, you asked for ‘grandchildren’ so I only put links to the $grandchild element. You are directly calling the ‘Child’ page so that becomes a Parent and the ‘grandchildren’ become childs in this case. To fix that we will add a link to the original ‘child’ element as well then, I didn’t think of doing it as I thought we where targeting grandchildren only :D.

    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

    Thread Starter enricolino

    (@enricolino)

    Man, you are great!!!!! Do you think you could help me with a last issue? I guess it should be in the initial part of the code but I’m not sure…. Could I get the title of the “subpage” too? I mean, I’d like it to look like here:

    https://www.neotropicalfungi.com/wp/fungal-species/undetermined/

    with the title and the lines underneath it….

    Thanks again for your help and patience ??

    Ciao

    Enrico

    That is something that your theme does by the CSS styles, but by following the code on your page

    If you alter this (pretty much at the start of the code)

    From :

    
    global $post;
    ?>
       <ul>
    

    To this, it should do the trick :

    
    global $post;
    ?>
        <header class="entry-header">
            <h1 class="entry-title" itemprop="headline"><?php echo the_title(); ?></h1>
        </header>
        <ul>
    

    For any extra styles etc you’ll have to search within the theme you are using to adjust them to fit perfectly ?? .

    Best regards,
    Konstantinos

    Thread Starter enricolino

    (@enricolino)

    Fantastic, it works! Thanks a lot :)!!!!!!

    Cheers

    Enrico

    No problem, glad I could help ??

    If you need anything else feel free to ask in a new topic.

    Best regards,
    Konstantinos

    Thread Starter enricolino

    (@enricolino)

    Hey, Xenos, excuse me again… I am trying to integrate your code into the original page.php file of the theme (obviously using a child theme), which would make the page template be perfect and look exactly like the others, but somehow I only manage to show the list of grandchild pages on top of the child-page title, or not at all. I was hoping I would manage to do it on my own, but I am obviously putting your code in the wrong place and also the wrong way :(. Only if you still have some minutes, could you please help again? The page code is:

    <?php
    /**
     * The template for displaying all pages.
     *
     * This is the template that displays all pages by default.
     * Please note that this is the WordPress construct of pages
     * and that other 'pages' on your WordPress site will use a
     * different template.
     *
     */
    
    get_header(); ?>
    
    	<div id="primary" class="content-area">
    		<main id="main" class="site-main" role="main" <?php hybrid_attr( 'content' ); ?>>
    
    			<?php while ( have_posts() ) : the_post(); ?>
    
    				<?php get_template_part( 'content', 'page' ); ?>
    
    				<?php
    					// If comments are open or we have at least one comment, load up the comment template
    					if ( comments_open() || '0' != get_comments_number() ) :
    						comments_template();
    					endif;
    				?>
    
    			<?php endwhile; // end of the loop. ?>
    
    		</main><!-- #main -->
    	</div><!-- #primary -->
    
    <?php get_footer(); ?>

    Thanks one more time….

    Best regards

    Enrico

    Hello again Enrico!

    If you modify the code that you gave me above, even if it is a ‘child’ template it will change all of your ‘pages’ structure, and I don’t think that’s something you want to do.

    First of all let me explain some things from what I see from this template:

    1] Everything in the code you gave me is a ‘general’ structure for all pages.

    2] The actual content though is loaded through this line <?php get_template_part( 'content', 'page' ); ?> . So there should be a file named content or it should be declared somehow in your code.

    3] To display your data correctly you will have to edit that ‘content’ page and not the general one.

    Now that this has been explained that is why I made you a ‘test’ template in the first place. This way you will get all the html code from both the page you show me above AND ‘content’ template and combine them to a new one.

    That way from your Admin when you create a ‘list’ page you can assign to it your custom template that will work ONLY for that page. Else all of your wordpress will have the same output, that’s something that you don’t want ( for example your Bio, you don’t need children/grandchildren in there ).

    I will try to get your theme later today and see how it works to give you either a good example or even a full solution. I’m with family at the moment so my hands are full hahah !

    Best regards,
    Konstantinos

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘Display grandchild pages on a child page’ is closed to new replies.