【问题标题】:WP_Query outputting twice even though there is one result即使有一个结果,WP_Query 也会输出两次
【发布时间】:2016-10-20 11:45:09
【问题描述】:

我在 apache 环境中有一个 wordpress 站点(由其他人构建),该站点已移至我们的 windows 服务器,我有一个奇怪的问题,即一个简单的 PhP/WP 循环两次输出相同的结果,即使有数据库中查询的一个结果。作为一名 MVC/NET 开发人员,我不确定下一步该去哪里,因为我无法调试(单步执行)代码。

<?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; ?>

关于可能导致此问题的原因或我如何设置循环限制的任何想法?

【问题讨论】:

    标签: php mysql wordpress templating


    【解决方案1】:

    试试这个:

    <?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; ?>
    

    【讨论】:

      猜你喜欢
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2019-11-12
      • 2023-03-21
      • 2020-12-11
      • 2021-01-16
      相关资源
      最近更新 更多