Michele Michele - 1 year ago 43
PHP Question

wordpress custom page template displaying posts twice

I'm not a wordpress expert, so wondering if someone can help me. I created a custom page template for one page so I could aggregate and display posts from one category on that page. However, I created one post in that category so far, but it gets displayed twice on the page. The posts that get pulled in from the category get displayed in the "div id=main" div. Here is the page template code:

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

//<?php while ( have_posts() ) : the_post(); ?>
<div id="title-bar">
<h1 class="bar-entry-title container"><?php the_title(); ?></h1>
<?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. ?>
// If comments are open or we have at least one comment, load up the comment template
if ( comments_open() || '0' != get_comments_number() )

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

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

Do you know why it is displaying the post twice? I can't see why.

Answer Source

You are running two loops. Your comment line is outside of the PHP on the 5th line.

Change this //<?php while ( have_posts() ) : the_post(); ?>

to this <?php // while ( have_posts() ) : the_post(); ?>

Also comment out the first <?php endwhile; ?> around line 9 or else you will get a PHP error