0

我不是wordpress专家,所以想知道是否有人可以帮助我。我为一个页面创建了一个自定义页面模板,这样我就可以在该页面上聚合和显示一个类别的帖子。但是,到目前为止,我在该类别中创建了一篇文章,但它在页面上显示了两次。从类别中提取的帖子将显示在“div id=main”div 中。这是页面模板代码:

<?php /* Template Name: Deals Page */ ?>
<?php get_header(); ?>

</div>
//<?php while ( have_posts() ) : the_post(); ?>
<div id="title-bar">
    <h1 class="bar-entry-title container"><?php the_title(); ?></h1>
</div>  
<?php endwhile; ?>  
<div class="container col-md-12"><!-- restart previous section-->

<div id="primary" class="content-area col-md-8">
    <main id="main" class="site-main" role="main">



                  <?php query_posts('category_name=deals &posts_per_page=20'); ?>
        <?php while ( have_posts() ) : the_post(); ?>
                             <?php the_title( '<h2>', '</h2>' ); ?>
                              <?php the_content(); ?>
            //<?php get_template_part( 'content', 'page' ); ?>



        <?php endwhile; // end of the loop. ?>
                 <?php
                // If comments are open or we have at least one comment, load up the comment template
                if ( comments_open() || '0' != get_comments_number() )
                    comments_template();
            ?>

    </main><!-- #main -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_sidebar('footer'); ?>
<?php get_footer(); ?>

你知道为什么它显示帖子两次吗?我不明白为什么。

4

1 回答 1

1

您正在运行两个循环。您的注释行在第 5 行的 PHP 之外。

改变这个//<?php while ( have_posts() ) : the_post(); ?>

对此<?php // while ( have_posts() ) : the_post(); ?>

还要注释掉第<?php endwhile; ?>9 行左右的第一个,否则你会得到一个 PHP 错误

于 2014-11-06T23:26:42.627 回答