rachael-portier
5/13/2019 - 8:50 PM

Shortcode to Get Thumbnail and Title of Child Pages in List While on Parent Page

// Product subcats on category page shortcode 
function shortcode_product_subcat() {
   
    ob_start(); 
    
    global $post;
    
    $id = get_the_ID();
    
$child_pages_query_args = array(
    'post_type'   => 'page',
    'post_parent' => $id,
    'posts_per_page' => -1,
    'orderby'=> 'title',
    'order' => 'ASC'
);

$child_pages = new WP_Query( $child_pages_query_args );
    
    echo '<ul class="product-page-list">';

if ( $child_pages->have_posts() ) : while ( $child_pages->have_posts() ) : $child_pages->the_post();
    ?>
    <li>
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>
    <?php
endwhile; endif;

// Be kind; rewind
wp_reset_postdata();
    
    echo '</ul>';
    
    return ob_get_clean();
}

add_shortcode('product_subcategory_with_image', 'shortcode_product_subcat');