admin管理员组

文章数量:1326011

I am facing a problem with the loop inside the page template. Here is the code.

<?php
/* Template Name: Blog-Template */

get_header();

$args = [
 'post_type' => 'post',
 'posts_per_page' => 1,
];

$queryP = new WP_Query( $args );

if ($queryP->have_posts()) {
    while ( $queryP->have_posts() ) : $queryP->the_post();
        ?>
        <article>
            <?php

            the_title( '<h1>', '</h1>' );
            the_excerpt();

            ?>
        </article>
        <?php
    endwhile;
}

get_footer();

If I set this page as a blog page in settings then no problem happens. But when I create a custom loop for this template it doesn't work. It shows nothing just header and footer.

I am facing a problem with the loop inside the page template. Here is the code.

<?php
/* Template Name: Blog-Template */

get_header();

$args = [
 'post_type' => 'post',
 'posts_per_page' => 1,
];

$queryP = new WP_Query( $args );

if ($queryP->have_posts()) {
    while ( $queryP->have_posts() ) : $queryP->the_post();
        ?>
        <article>
            <?php

            the_title( '<h1>', '</h1>' );
            the_excerpt();

            ?>
        </article>
        <?php
    endwhile;
}

get_footer();

If I set this page as a blog page in settings then no problem happens. But when I create a custom loop for this template it doesn't work. It shows nothing just header and footer.

Share Improve this question edited Sep 1, 2020 at 16:13 Raashid Din asked Sep 1, 2020 at 13:30 Raashid DinRaashid Din 2182 silver badges19 bronze badges 9
  • You dont have if ( $query->have_posts() ) condition before your while loop. Try to add this and then check again if it's working – MMPL1 Commented Sep 1, 2020 at 13:49
  • you can check the code again, no query variable used – Raashid Din Commented Sep 1, 2020 at 13:50
  • Normally loops have an if ( $posts->have_posts() ) check so that they can display a no posts found type message if there are no posts, but the code here doesn't do that, so there's no way to know if it's because it found no posts, or if it found them but didn't display them. Also, is there a reason you discarded the main query and doubled the amount of work the database had to do, instead of modifying the main query with pre_get_posts so it returned what you wanted? – Tom J Nowell Commented Sep 1, 2020 at 13:50
  • Also, the default post type is post and the default status is publish so you don't need to put those in your query. It also needs too do cleanup afterwards – Tom J Nowell Commented Sep 1, 2020 at 13:51
  • i added if statement but still not working – Raashid Din Commented Sep 1, 2020 at 13:56
 |  Show 4 more comments

2 Answers 2

Reset to default 0

From https://wordpress/support/article/creating-a-static-front-page/,

Posts Page: (if not already created) create an empty page. Give it a Title that will be used on top of your posts list. This step is mandatory as you are modifying the WordPress default setting. Any other content other than Title will no be displayed at all on this specific page.

So, When you set it as Posts page, Your loop is ignored.

You can set it as static front page (Homepage) or a normal page with custom page template.

It solved automatically. I think the WordPress version 5.5.1 is full of bugs. Thanks, everyone for your support.

本文标签: theme developmentLoop inside page template not working