0

Хочу вывести записи в wordpress, используя данную конструкцию:

        <div class="primary">
            <?php if (have_posts()) {while (have_posts( )) {the_post(); ?>
                <article>
                    <h2><?php the_title(); ?></h2>
                    <?php the_content(); ?>
                </article>
            <?php }
            } ?>
        </div>

Но вместо записей выводятся страницы. Как мне исправить это?

1 ответ 1

2

Используйте для вывода класс WP_Query

        <div class="primary">

            <?php $args = []; ?>
      
            <?php $args['post_status'] = 'publish'; ?>
                
            <?php $args['post_type'] = 'post'; ?>
                        
            <?php $args['orderby'] = 'date'; ?>

            <?php $args['order']  = 'DESC'; ?>
                
            <?php $the_query = new \WP_Query($args);  ?>
 
            <?php if ($the_query->have_posts()) {while ($the_query->have_posts()) {$the_query->the_post();  ?>
                <article>
                    <h2><?php the_title(); ?></h2>
                    <?php the_content(); ?>
                </article>
            <?php }
            } ?>
            
            <?php wp_reset_postdata(); ?>
        </div>

Вывод записей определяет параметр $args['post_type'] = 'post';

Подробнее тут https://wp-kama.ru/function/wp_query

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.