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

时间:2015-12-16 09:14:28

标签: php html 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();?>

我错过了什么?

1 个答案:

答案 0 :(得分:2)

你需要添加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 );

你应该在

之前添加这段代码
if (have_posts()):
while (have_posts()): the_post();?>

关于have_posts()的这部分将是

// The Loop
if ( $query->have_posts() ) { ?>
    <?php while ( $query->have_posts() ) {
        $query->the_post();

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