WordPress.org

Ready to get started?Download WordPress

Forums

Limit get_pages or get_children showing grandchildren (3 posts)

  1. Chintaru
    Member
    Posted 2 years ago #

    Hello,

    I am trying to list page descendants by their attachments/featured images and have succeeded mostly. What I can't seem to accomplish is to display the last 10 thumbnails of each page on their (great-)grandparent's page. Here's my structure:
    ____________________________________
    Main page
    - Child 1
    - - Grandchild 1*
    - - Grandchild 2*
    - - Grandchild 3*
    - Child 2
    - - Grandchild 1
    - - - Great-grandchild 1*
    - - - Great-grandchild 2*
    - - - Great-grandchild 3*
    - - Grandchild 2
    - - Grandchild 3
    - - etc
    ____________________________________
    * Only these pages have thumbnails/featured images.

    And this is how it should look like:
    ____________________________________
    Child title
    [GC] [GC] [GC]

    Child 2 title
    [GGC] [GGC] [GGC]
    ____________________________________
    With [GC] and [GGC] being the grandchild and great-grandchild thumbnails.

    So I want to list the thumbnails of each child's (grand)children pages when I'm on the main page. I know it sort of can be done with get_pages, but the problem is that it shows ALL the thumbnails of the descendants and I just want to show the last 10. get_children has the numberposts parameter which works, but it only shows the grandchildren and not the great-grandchildren thumbnails. Is there any way to accomplish this? Unfortunately get_page's number parameter doesn't work at all.

  2. Chintaru
    Member
    Posted 2 years ago #

    Anyone?

  3. Chintaru
    Member
    Posted 2 years ago #

    In case anyone is wondering... After some investigating, I discovered that the reason why get_pages' number parameter doesn't work, is because get_pages fetches the pages, limits them based on number and THEN checks whether the fetched pages are children. So in my case, no pages showed up because the childpages were not in the first 10 results set as the limit.

    A little elaboration: what get_pages does basically, is get ALL pages, then filters them depending on your parameters. So what I did to limit my results properly, is use a foreach loop (this is because my results aren't always expected to be direct children, or with a post thumbnail - hence number not working) with a counter, a condition so it loops until the counter reaches the set limit and then break the loop so it doesn't iterate over results I don't need (outside of the limit). Like so:

    $maxItems = 10;
    	$count = 0;
    	$pages = get_pages($args);
    
    	foreach($pages as $page) {
    		if(has_post_thumbnail($page->ID) && $count < $maxItems ) {
    			$thumb = get_the_post_thumbnail($page->ID, 'thumbnail', $attr = 'title='.$page->post_title);
    			$count++;
    			?>
    				<div class="item">
    					<a href="<?php echo get_page_link($page->ID) ?>"><?php echo $thumb; ?><span class="new"></span></a>
    				</div>
    			<?php
    		} elseif( $count >= $maxItems ) {
    			break;
    		}
    	}

    Hope that helped anyone :)

Topic Closed

This topic has been closed to new replies.

About this Topic