It is necessary to display posts from different headings on one page, but when I indicate heading 2 and 3 in the code, then all posts from all headings are displayed in them? In the recording of the question mark, the rubrics are correct, what could be the problem?

<?php $top_query = new WP_Query('showposts=-1'); ?> <?php if ( have_posts() ) : query_posts('cat=2'); // указываем ID рубрик, которые необходимо вывести. while($top_query->have_posts()) : $top_query->the_post(); $first_post = $post->ID; ?> <div class="news-item"> <a href="<?php the_date('dmY' ); ?>"><?php the_post_thumbnail('full');?></a> <h4 class="ni-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4> <p class="ni-desc"><?php the_excerpt(); ?></p> <div class="ni-footer clearfix"> <a href="<?php the_permalink(); ?>" class="ni-more">читать далее</a> <span class="ni-date"><?php the_date('dmY' ); ?></span> </div> </div> <?php endwhile; ?> <?php else : ?> <?php endif; ?> <?php wp_reset_query(); ?> </div> <div class="middle-news-block clearfix"> <?php $top_query = new WP_Query('showposts=-1'); ?> <?php if ( have_posts() ) : query_posts('cat=3'); // указываем ID рубрик, которые необходимо вывести. while($top_query->have_posts()) : $top_query->the_post(); $first_post = $post->ID; ?> <div class="middle-news-item"> <a href="<?php the_permalink(); ?>"> <?php the_post_thumbnail('full');?> <h2 class="mni-title"><?php the_title(); ?></h2> <span class="mni-date"><?php the_date('dmY' ); ?></span> </a> </div> <?php endwhile; ?> <?php else : ?> <?php endif; ?> <?php wp_reset_query(); ?> </div> 

    1 answer 1

    I don’t really remember, but it seems that you either use the WP_Query class or query_posts (I can’t accurately express the idea, correct if something query_posts wrong):

     // Пример 1: Запрос с query_posts query_posts( $args ); while ( have_posts() ) { the_post(); the_title(); // вывести название } // Пример 2: Запрос с помощью WP_Query $second_query = new WP_Query( $args ); while ( $second_query->have_posts() ) { $second_query->the_post(); the_title(); } 

    Picture in topic

    Therefore, in your code you need to change two lines:

     $top_query = new WP_Query(array('showposts'=>-1, 'cat'=>'2')); ... $top_query = new WP_Query(array('showposts'=>-1, 'cat'=>'3')); 

    To specify multiple categories:

     $top_query = new WP_Query(array('showposts'=>-1, 'cat'=>'2,3,4')); 

    or

     $top_query = new WP_Query(array('showposts'=>-1, 'category__in'=>array(2,3,4))); 
    • And if I need to withdraw from the three headings? - Bim Bam
    • In general, there is excellent documentation where you can find the answers - codex.wordpress.org/Class_Reference/ ... There are options: new WP_Query( array( 'cat' => '2,6,17,38' ) ); or new WP_Query( array( 'category__in' => array( 2, 6 ) ) ); - Bookin
    • Can you help me implement it in the code, otherwise I don’t quite understand where to insert it ...? - Bim Bam
    • What is your difficulty? - Bookin
    • I don’t understand how to put your example in your code so that it works ... - Bim Bam