简体   繁体   中英

Custom Post Type is only returning the first post

Here is my registered post type


add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'team',
        array(
            'labels' => array(
                'name' => __( 'Team Members' ),
                'singular_name' => __( 'Team Member' )
            ),
        'public' => true,
        'url_slug' => 'company/'
        )
    );
}

and here is my loop

 <?php
$args = array(
  'post_type'   => 'team',
  'post_status' => 'publish',
  'posts_per_page' => -1,
    );
    $query = new WP_Query($args);
    if ( $query->have_posts() ){ 
        print_r($query);
        while ( $query->have_posts() ) $query->the_post(); {

            ?>
            do what I want here 
            <?php

        }
    }
    wp_reset_postdata();
 ?>

It outputs the first post fine. If I print_r($query) it returns all posts but I cannot get it to loop through for some reason.

change the loop code to

 <?php
$args = array(
  'post_type'   => 'team',
  'post_status' => 'publish',
  'posts_per_page' => -1,
    );
    $query = new WP_Query($args);
    if ( $query->have_posts() ){ 

        while ( $query->have_posts() ) $query->the_post(); {

            ?>
            print_r($query);
            do what I want here 
            <?php

        }
    }
    wp_reset_postdata();
 ?>

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