• Resolved NeiltheSpacechimp

    (@neilthespacechimp)


    Hi all,

    I’m approaching a deadline and I’m hoping someone has done this before and will be able to provide a shortcut to the answer. I have had a look around the forum and google but can’t find the answer I apologise if this has been covered elsewhere.

    I have created a custom post type and populated it. I use a wp_query to display the custom post types featured images on the home page as a slider.

    I’ve set the CPT to hierarchical – true and support Page-attributes I’ve set some of the CPT posts now have children, I would like to display an image slider populated with it’s children’s featured images.

    I hope that makes sense and thanks in advance to anyone who can help.

Viewing 1 replies (of 1 total)
  • Thread Starter NeiltheSpacechimp

    (@neilthespacechimp)

    I have this working as I wanted now, for future reference I will share the code I used which I adapted from Russ’ answer here. Add this to your single-cpt.php file in your template theme, or create one if you don’t already have one.

    <?
    $child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'CPT' ORDER BY menu_order", 'OBJECT');
    
    if ( $child_pages ) :
        foreach ( $child_pages as $pageChild ) :
            setup_postdata( $pageChild );
            $thumbnail = get_the_post_thumbnail($pageChild->ID, 'thumbnail');
            if($thumbnail == "") continue; // Skip pages without a thumbnail
    ?>
            <div class="child-thumb">
              <a href="<?= get_permalink($pageChild->ID) ?>" rel="bookmark" title="<?= $pageChild->post_title ?>">
                <?= $pageChild->post_title ?><br /><?= $thumbnail ?>
              </a>
            </div>
    <?
        endforeach;
    endif;
    ?>

    Replace where it says post_type = ‘CPT’ with your own custom post type name and if you want to make an image slider like I did change the div child-thumb to match the layout required by the slideshow or carousel you are using.

    In order for this solution to work you must set Hierarchial to true and supports must include page-attributes within the CPT settings in your functions.php file ie:
    'hierarchical' => true 'supports' => array('title', 'editor', 'thumbnail', 'page-attributes'),

    You will also want to set your post type to be ordered by menu order by adding this to your functions.php file

    //Order custom post type by menu order (must have supports page-attribute)
    function CPT_archive_order( $vars ) {
      if ( !is_admin() && isset($vars['post_type']) && post_type_supports($vars['post_type'], 'page-attributes') ) {
        $vars['orderby'] = 'menu_order';
        $vars['order'] = 'ASC';
      }
    
      return $vars;
    }
    add_filter( 'request', 'CPT_archive_order');

    I hope that helps some else in the future.

    Spacechimp out

Viewing 1 replies (of 1 total)
  • The topic ‘List child CPT featured items inside parent’ is closed to new replies.