WordPress.org

Support

Support » How-To and Troubleshooting » Cycling through grandchildren pages

Cycling through grandchildren pages

  • nathan12343

    @nathan12343

    I’ve got a set up of 3 tiers of pages in the site structure. A bit like this:

    • Home
    • Category
      • Section
        • Page of content
        • Another page of content
      • Another section
        • More content
    • Category 2
      • A section in here
        • Even more pages

    I’ve got a link at the bottom of the pages of content to the next page. What I want to do is to bridge the gap between the sections and also the categories. So when you get to the bottom of “Another page of content” you get a link to go to “More content”. Also at the bottom of “More content” there is a link to “Even more pages”. Effectively all of the grandchildren pages.

    This is what I’m using for the “Next” link within the same parent:

    <?php
    $pagelist = get_pages('sort_column=menu_order&sort_order=asc&child_of='.$post->post_parent);
    $pages = array();
    foreach ($pagelist as $page) {
       $pages[] += $page->ID;
    }
    
    $current = array_search($post->ID, $pages);
    $nextID = $pages[$current+1];
    $nexttitle = get_the_title($nextID);
    ?>
    <?php if (!empty($nextID)) { ?>
    Next: <a href="<?php echo get_permalink($nextID); ?>"><?php echo $nexttitle ?></a>
    <?php } ?>

    This is a bit long winded, I hope it makes sense – I hope someone can help me!! Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • nathan12343

    @nathan12343

    I’ve been doing a lot of digging and found that I can have parent=0 as an argument to get all of the top level pages. So I figured that if I collect all of the top level pages I can those as includes for the child_of arguement to get the second level. Then pass this into my final get_pages() as child_of to replace .$post->pages to give me all of the third levels.

    e.g.

    $firsttier = get_pages('parent=0');
    $secondtier = get_page('child_of=' .$firsttier):
    $pagelist = get_pages('sort_column=menu_order&sort_order=asc&child_of='.$secondtier);
    
    $pages = array();
    foreach ($pagelist as $page) {
       $pages[] += $page->ID;
    }

    But this doesn’t work.

    Can anyone see where I’m going wrong?

    brockangelo

    @brockangelo

    I would double check that the return values are returning what you are expecting. Do a print_r on both of your variables to see what it is returning. like: print_r($secondtier).

    Here is what I came up with. Let me know if it helps. A word of caution, this loads all top three levels of pages into memory, so it goes dreadfully slow on our intranet (2,000+ pages):

    <?php 
    
    $first_tier = get_pages('parent=0');
    $second_tier = array();
    $third_tier = array();
    
    foreach ($first_tier as $first_tier_page) {
       $second_tier = get_pages('child_of'.$first_tier_page->ID);
       foreach ($second_tier as $second_tier_page) {
    		$third_tier = get_pages('child_of'.$second_tier_page->ID);
    		}
    	}
    
    	$id = $third_tier[0];
    	echo get_the_title($id);
    
    ?>

    I put a sample call at the end of how to fetch a title. Good luck!

    nathan12343

    @nathan12343

    Hi brockangelo,

    Really appreciate you looking into this. The first tier bit worked a charm (only returned the top level, but unfortunately second tier threw everything back, as did the third tier.

    I added ‘child_of=0’ to the first tier code. I tried changing ‘child_of’ to ‘parent’ but this didn’t seem to have any impact.

    I’ve got a really ugly work around going but would love to get this sussed properly!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Cycling through grandchildren pages’ is closed to new replies.
Skip to toolbar