内容和标题不会显示在Wordpress的某些页面上

Mic*_*ael 6 html php wordpress loops

我有一个"front-page.php",这是一个静态的单面页面.如果我使用Wordpress循环在front-page.php上查看我的最新帖子,他们都会显示出来.现在我想创建一个新闻页面,所以我创建了一个文件"page-news.php".从首页删除循环代码并将其粘贴到页面新闻中.虽然没有任何反应.

循环代码:

<?php get_header();?>

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

<?php the_title();?>
<?php the_content();?>

<?php
endwhile;
else: echo '<p>no posts were found</p>';
endif;

 ?>

<?php get_footer();?>
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

J.T*_*ral 1

您需要添加 wp_Query 主页被视为博客页面,因此它具有查询默认值。

$args = array (
/*'cat'                    => $catNum,*/
'post_type'              => 'post',
'pagination'             => false,
'posts_per_page'         => '-1',
'ignore_sticky_posts'    => false,
'order'                  => 'DESC',
'orderby'                => 'date',
);

// The Query
$query = new WP_Query( $args );
Run Code Online (Sandbox Code Playgroud)

你应该在之前添加此代码

if (have_posts()):
while (have_posts()): the_post();?>
Run Code Online (Sandbox Code Playgroud)

这部分内容have_posts()

// The Loop
if ( $query->have_posts() ) { ?>
    <?php while ( $query->have_posts() ) {
        $query->the_post();
Run Code Online (Sandbox Code Playgroud)

wp_reset_postdata();不要忘记在末尾添加,这样您就可以在一个页面中使用多个查询。