Невозможно пройти через пользовательский тип сообщения - PullRequest
0 голосов
/ 25 марта 2019

У меня есть пользовательский тип записи, называемый external_press, и когда я пытаюсь просмотреть его и отобразить the_title(), все, что я получаю, это заголовки из сообщений. Я работаю в файле index.php.

Вот мой тип регистрации сообщений:

function custom_post_type_init() {
  register_post_type( 'external_press',
    array(
      'labels' => array(
        'name' => __( 'External Press' ),
        'singular_name' => __( 'Press' )
      ),
      'public' => false,  // it's not public, it shouldn't have it's own permalink, and so on
      'publicly_queryable' => true,  // you should be able to query it
      'show_ui' => true,  // you should be able to edit it in wp-admin
      'exclude_from_search' => true,  // you should exclude it from search results
      'show_in_nav_menus' => false,  // you shouldn't be able to add it to menus
      'has_archive' => false,  // it shouldn't have archive page
      'rewrite' => false,  // it shouldn't have rewrite rules
      'menu_position' => 5, // bellow posts
      // 'supports' => array( // only show title and thumnail
      //   'title',
      //   'thumbnail'
      // ),
    )
  );
}
add_action( 'init', 'custom_post_type_init' );

Вот как я пытаюсь отобразить заголовок пользовательских типов сообщений

$args = new WP_Query(array(
            'post_type' => 'external_press',
        ));

        // the query
        $the_query = new WP_Query( $args ); ?>

        <?php if ( $the_query->have_posts() ) : ?>

            <!-- pagination here -->

            <!-- the loop -->
            <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                <h2><?php the_title(); ?></h2>
            <?php endwhile; ?>
            <!-- end of the loop -->

            <!-- pagination here -->

            <?php wp_reset_postdata(); ?>

        <?php else : ?>
            <p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
        <?php endif; ?>

1 Ответ

1 голос
/ 25 марта 2019

Вы объявляете свой WP_Query дважды.

См. Ниже пример цикла.

<?php $loop = new WP_Query( array( 'post_type' => 'external_press' ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <h2><?php the_title(); ?></h2>
<?php endwhile; wp_reset_query(); ?>

Подробнее здесь: https://codex.wordpress.org/Post_Types#Querying_by_Post_Type

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...