There is a blog page, and here I am trying to display posts, but when I write this if ( have_posts() ) : query_post... then the page is empty, nothing is loaded into it at all, I call page-blog.php page maybe a problem? In the admin panel, I go to the Blog page and switch to the Blog template (since we are at the top of page-blog.php. We gave the name of the Blog template.)

page-blog.php

  <?php /* Template Name: Blog */ get_header(); ?> <article> <section class="mblog"> <div class="container"> <div class="mblog__wrapper"> <?php if ( have_posts() ) : query_posts('cat=6'); while (have_posts()) : the_post(); ?> <div class="mblog__col"> <a href="<?php the_permalink(); ?>" class="mblog__post-item"> <div class="mblog__post-img-wp js-bg-img"> <?php the_post_thumbnail();?> </div> <div class="mblog__post-title"><?php the_title(); ?></div> <div class="mblog__post-content"><?php the_content(); ?></div> </a> </div> <? endwhile; endif; wp_reset_query(); ?> </div> </div> </section> </article> <?php get_footer(); ?> 
  • What does "page I call page-blog.php" mean ? Where do you call it that? - Pyramidhead
  • one
    Here wpmag.ru/2014/query_posts-wordpress explain in detail why you should never use query_posts . - Pyramidhead
  • page-blog.php in the root of the project where all my files are, Index.php, function.php, header.php and this one, I thought that the pages I created need to be inserted page -... - Dima Vleskov
  • then you can tell what the cycle will be like here then how to write it correctly - Dima Vleskov
  • one
    @DimaVleskov codex.wordpress.org/… Ida - read about the danger of query_posts - SeVlad

1 answer 1

it was correct to use another loop, namely new WP_Query

 <?php /* Template Name: blog */ get_header(); ?> <article> <section class="mblog"> <div class="container"> <div class="mblog__wrapper"> <?php $query = new WP_Query( 'cat=6' ); ?> <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?> <div class="mblog__col"> <a href="<?php the_permalink() ?>" class="mblog__post-item"> <div class="mblog__post-img-wp js-bg-img"> <?php the_post_thumbnail();?> </div> <div class="mblog__post-title"><?php the_title(); ?></div> <div class="mblog__post-content"><?php the_content(); ?></div> <div class="mblog__post-footer"> <div class="mblog__post-date"><?php the_time( 'F jS, Y' ); ?></div> <div class="mblog__post-comment"> <svg class="mblog__post-comment-icon"> <use xlink:href="#comment"></use> </svg> <span class="mblog__post-comment-number">10</span> </div> </div> <div class="mblog__post-overlay"> <span class="mblog__post-btn">Read Article</span> </div> </a> </div> <?php endwhile; wp_reset_postdata(); else : ?> <p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p> <?php endif; ?> </div> </div> </section> </article> <?php get_footer(); ?>