• i have a db query using get_results that is successfully pulling all child pages of a particular parent page.

    i am having trouble converting the code into a new loop that pulls all grandchildren pages of that same ancestor page.

    here is the code that i have working for child pages:

    <?php
    $results = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish'");
    $child_pages = $wpdb->get_results("SELECT *  FROM $wpdb->posts WHERE post_parent = ".$post->ID."   AND post_type = 'page' ORDER BY ID", 'OBJECT'); ?>
    <?php
    if (!empty ( $child_pages )) : ?>
    <? endif; ?>                  
    
    		<h3 class="category-heading"><?php the_title(); ?></h3>
    
    		<div id="featured-container">
    
    			<div class="flexslider">
    
              			<ul class="slides">
    
    <?php $i = 0; if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
                        <li>
                                <?php $post_image_id = get_post_thumbnail_id($pageChild->ID);
    		if ($post_image_id) {
    			$sliderBg = wp_get_attachment_image_src( $post_image_id, 'large', false);
    			if ($sliderBg) (string)$sliderBg = $sliderBg[0];
    		}
    		?>
                                <a class="slider-img" href="<?php echo  get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>" style="background: url(<?php echo $sliderBg ?>) top left no-repeat;"></a>
    <div class="featured-box">
    
    							<h2><a href="<?php echo  get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></h2>
    
    							<p><?php echo $pageChild->post_excerpt; ?></p>
    
    						</div><!--featured-box-->
    
    </li>
    <?php endforeach; endif; ?>
    
    </ul><!-- slides -->
    </div><!-- flexslider -->
    </div><!-- featured container -->
    <?php endif; ?>
    		<?php endwhile; endif; ?>

    i have tried some conventional attempts to place a loop within a loop, but the get_results approach is giving me trouble.

    any help is appreciated!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter fatsandrew7

    (@fatsandrew7)

    **** UPDATE ****

    still needing help with this.

    i have modified the code which brought me closer to my goal.

    here is the code:

    <?php
    $mypages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );
    
    			foreach( $mypages as $page ) {
    
    			if($page->post_parent != $post->ID){
    				$content = $page->post_content;
    				//if ( ! $content ) // Check for empty page
    					//continue;
    
    				$content = apply_filters( 'the_content', $content );
    			?>
                        <li>
                                <?php $post_image_id = get_post_thumbnail_id($page->ID);
    		if ($post_image_id) {
    			$sliderBg = wp_get_attachment_image_src( $post_image_id, 'large', false);
    			if ($sliderBg) (string)$sliderBg = $sliderBg[0];
    		}
    		?>
                                <a class="slider-img" href="<?php echo get_permalink($page->ID); ?>" rel="bookmark" title="<?php echo $page->post_title; ?>" style="background: url(<?php echo $sliderBg ?>) top left no-repeat;"></a>
    <div class="featured-box">
    
    							<h2><a href="<?php echo get_page_link( $page->ID ); ?>" rel="bookmark" title="<?php echo $page->post_title; ?>"><?php echo $page->post_title; ?></a></h2>
    
    							<p><?php echo $page->post_excerpt; ?></p>
    
    						</div><!--featured-box-->
    
    </li>                    
    
    			<?php
    			}
    			}
    		?>

    this now successfully outputs the grandchild page content.

    the only problem now is limiting the output to only that grandchild level (3 levels deep from current page).

    is it possible to tell wordpress NOT to output the child pages of that 3rd level/grandchild?

    Thread Starter fatsandrew7

    (@fatsandrew7)

    does anyone have some insight into this?

    i feel like i am very close but seem to be missing that last piece that allows me to limit the depth of the loop.

    please let me know what direction i need to go from here — someone. ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘loop showing only grandchildren of ancestor pages’ is closed to new replies.