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 | Show 4 more comments2 Answers
Reset to default 0From 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
版权声明:本文标题:theme development - Loop inside page template not working 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742134465a2422308.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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 withpre_get_posts
so it returned what you wanted? – Tom J Nowell ♦ Commented Sep 1, 2020 at 13:50post
and the default status ispublish
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