[英]WordPress : using page template to fetch posts in wp page
我想在wp页面中显示帖子。 为此,我所做的就是创建了一个名为mypage-page.php的模板,并将代码从page.php复制到mypage-page.php。
这是我的mypage-page.php
<main id="main" class="site-main" role="main">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ;?></h2>
<?php the_post_thumbnail(); ?>
<?php the_excerpt(); ?>
<?php endwhile; else: ?>
<p>Sorry, no posts to list</p>
<?php endif; ?>
</main><!-- .site-main -->
// this has header and footer as well .
现在,我使用此模板创建了一个页面mytest ,我希望它应该列出帖子,但没有,请帮助我了解我错了。 我只是一个初学者
如果您的页面是“自定义页面”模板,则您已经初始化了一个自定义查询。 WordPress的默认查询不适用于自定义页面模板。
您可以使用WP_Query类进行该查询: https ://codex.wordpress.org/Class_Reference/WP_Query
或get_posts(): https : //codex.wordpress.org/Template_Tags/get_posts
我建议较早的一个。
您也可以使用此query_posts
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.