0

Here is a look at my current code to display all "children" pages of current post_parent (Also would be helpful to know how to display custom parent pages as well...)

<?php
$args = array(
    'post_parent' => $post->ID,
    'post_type' => 'page',
    'orderby' => 'menu_order',
    'order'=>'ASC',
);
$my_query = new WP_Query($args);
if($my_query->have_posts()) :
    while($my_query->have_posts()) : 
        $my_query->the_post();
      // your stuff goes in this bit.
     if ( has_post_thumbnail() ) : 
         $imgdata = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' );
         $imgwidth = $imgdata[1]; // thumbnail's width
         $imgheight = $imgdata[2]; // thumbnail's height
  ?>
   <div class="child element isotope-item" style="width:<?php echo $imgwidth;?>;height:<?php echo $imgheight;?>;">
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
      <?php echo the_post_thumbnail(large); ?>
      <span class="child_title"><?php echo the_title_attribute();?></span>
    </a>
  </div>
  <?php
    endif;
    // end your stuff.
    endwhile;
else :
    // Do the no posts found message 
endif;
?>
</div>
4
  • What is a "custom parent page"? You already have the $post object for the parent or this code wouldn't work. Display that object the same way you displaying the children. Or, maybe "exactly what do you want to do with the parent?"
    – s_ha_dum
    Jul 15, 2013 at 18:29
  • A custom parent page would be: I want "defined" parent pages to be displayed as featured images on this page -- But back to my main issue -- how do I just display parent pages (as opposed to my code displaying children pages of post_parent) - I want all of the parent items (not just the one I'm currently on) - to display featured images on a page to create an image navigation structure. Jul 15, 2013 at 18:35
  • When you say "all of the parent items", are you referring to all the posts returned by get_post_ancestors(), or all the "root" pages (ie, pages that have no ancestors)?
    – Pat J
    Jul 15, 2013 at 18:46
  • All of the "root" pages Jul 15, 2013 at 18:48

1 Answer 1

0

To get all the "root" pages of a site:

$args = array(
    'post_type' => 'page',
    'child_of' => 0,
);
$pages = get_pages( $args );
foreach( $pages as $page ) {
    if( has_post_thumbnail( $page->ID ) {
        $imgdata = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' );
        $imgwidth = $imgdata[1]; // thumbnail's width
        $imgheight = $imgdata[2]; // thumbnail's height
    }
}
// handle the image as in your posted code

To get your custom post type, you should be able to replace 'post_type' => 'page' with your CPT's name (eg, 'post_type' => 'my_post_type').

Reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.