【问题标题】:wordpress custom page template displaying posts twicewordpress 自定义页面模板显示帖子两次
【发布时间】:2014-11-06 23:19:24
【问题描述】:

我不是 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(); ?>

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

【问题讨论】:

  • 哦,是的 - 页面在这里:link
  • 此注释不起作用,因为它不在 php 代码中://&lt;?php get_template_part( 'content', 'page' ); ?&gt; 应该是 &lt;?php //get_template_part( 'content', 'page' ); ?&gt;

标签: php wordpress custom-pages


【解决方案1】:

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

更改此//&lt;?php while ( have_posts() ) : the_post(); ?&gt;

到这个&lt;?php // while ( have_posts() ) : the_post(); ?&gt;

还要注释掉第 9 行附近的第一个 &lt;?php endwhile; ?&gt;,否则会出现 PHP 错误

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多