简体   繁体   中英

How to display only the child posts of a custom post type?

Im having trouble trying to find a solution to display the child posts in a custom post type on the custom post type's archive page.

<?php wp_reset_postdata();
    // WP_Query arguments
    $args = array (
        'post_type'             => 'locations',
        'posts_per_page'        =>  -1,
    );

    // The Query
    $location_query = new WP_Query( $args );
?>
    <?php if ( $location_query->have_posts()): ?>
        <ul>
            <?php while ( $location_query->have_posts() ) : $location_query->the_post();
                $location = get_field('map');
            ?>

                <li>
                    <?php echo $location['address']; ?>
                </li>

            <?php endwhile; ?>
        </ul>
    <?php endif; ?>
<?php wp_reset_postdata(); ?>

You need to add the post_parent variable in your args. Though with only that code I can't see what you'd need to do to reference the post_id.

$args = array(
    'post_parent' =>  $post->ID,
    'posts_per_page' => -1,
    'post_type' => 'locations'
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM