F
F
falwxey2018-07-25 19:29:12
WordPress
falwxey, 2018-07-25 19:29:12

Question about cycles in wordpress?

Hello. Can you explain why the list of posts is not displayed on the main page. underscores theme. What I've done ; I took the page.php template file, duplicated it, renamed it to front-page.php, created the
"Home" page, inserted the layout there, everything works.
In the reading settings, I specified the static page "Home".
Got the cycle code

<div id="primary" class="content-area">
    <main id="main" class="site-main">

    <?php
    while ( have_posts() ) :
      the_post();

      get_template_part( 'template-parts/content', 'page' );

      // If comments are open or we have at least one comment, load up the comment template.
      if ( comments_open() || get_comments_number() ) :
        comments_template();
      endif;

    endwhile; // End of the loop.
    ?>

    </main><!-- #main -->
  </div><!-- #primary -->

inserted in the right place, but the posts are not displayed, only the title of the current page. When in the reading settings I change to @On the
main page to display: your last entries @ , then the cycle works. I roughly understand that the matter is in the hierarchy, but I can’t really understand it. Maybe you have a link to the "chewed" text or video, I will be very grateful

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2018-07-26
@azerphoenix

Hello!
About the template name...
front-page.php means a static page,
home.php means a block with the latest posts.
In this code, content is displayed inside the loop, for the page template-parts/content, page:

<?php
    while ( have_posts() ) :
      the_post();
      get_template_part( 'template-parts/content', 'page' );
    endwhile; // End of the loop.
    ?>

What exactly lies inside this template, I do not know. Look in the directory template-parts/content-page.php
And if you want to display posts on a static page, then do the following:
https://wp-kama.ru/function/wp_query
// задаем нужные нам критерии выборки данных из БД
$args = array(
  'posts_per_page' => 5,
);

$query = new WP_Query( $args );

// Цикл
if ( $query->have_posts() ) {
  while ( $query->have_posts() ) {
    $query->the_post();
    echo '<li>' . get_the_title() . '</li>';
  }
} else {
  // Постов не найдено
}
/* Возвращаем оригинальные данные поста. Сбрасываем $post. */
wp_reset_postdata();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question