M T M T - 1 month ago 8
MySQL Question

WP_Query outputting twice even though there is one result

I have a wordpress site (build by someone else) in an apache environment which has been moved to our windows server, I have a strange issue where a simple PhP/WP loop is outputting the same result twice, even though there is one result for the query in the DB. Being a MVC/NET developer I'm not sure where to go next as I cant debug(step through) the code.

<?php
if (have_posts()): while (have_posts()) : the_post();
$query = new WP_query('pagename=about');
$query->the_post();
/* Page Content */
echo '<h2 class="heading">';
the_title();
echo '</h2>';
echo '<div class="content">';
the_content();
echo '</div>';

?>

<?php endwhile; ?>
<?php endif; ?>


Any ideas of what could be causing this or how I could set a loop limit?

Answer

Try this:

<?php 
    $query = new WP_query('pagename=about'); 
    if ($query->have_posts()): while ($query->have_posts()) :    
    $query->the_post();
    /* Page Content */
    echo '<h2 class="heading">';
    the_title();
    echo '</h2>';
    echo '<div class="content">';
    the_content();
    echo '</div>';            

?>

<?php endwhile; ?>
<?php endif; ?>