【问题标题】:wordpress multiple loops problemwordpress 多循环问题
【发布时间】:2009-10-01 17:00:01
【问题描述】:

我正在尝试建立一个 single.php 页面。此页面用于显示完整的单个帖子。

我有 3 个循环。前两个用于(每个)从特定类别中获取随机帖子。

1

<?php query_posts(array('orderby' => 'rand', 'category_name' => announcement, 'showposts' => 1)); if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>

2

<?php query_posts(array('orderby' => 'rand', 'category_name' => quote, 'showposts' => 1)); if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>

问题在于应该显示真实帖子本身的第三个循环。

3

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; else: ?>
<?php endif; ?>

^^ 这显示与 #2 相同的帖子。

所以,#1 和 #2 工作得很好,但是如何让 #3 显示它应该显示的单个帖子 - 例如,来自链接 http://example.com/one-post/ 标题为 one-post 的帖子应该被显示。

#1 和#2 位于页面的“顶部”区域,而#3 应位于页面中间。

【问题讨论】:

    标签: php wordpress loops


    【解决方案1】:

    固定。

    修复它。将 #1 和 #2 更改为

    <?php
        $randomAnnouncement = new WP_Query();
        $randomAnnouncement->query('cat=545&showposts=1&orderby=rand');
        while ($randomAnnouncement->have_posts()) : $randomAnnouncement->the_post();
    ?>
    
    <?php the_content(); ?>
    
    <?php endwhile; ?>
    

    <?php
        $randomQuote = new WP_Query();
        $randomQuote->query('cat=546&showposts=1&orderby=rand');
        while ($randomQuote->have_posts()) : $randomQuote->the_post(); ?>
    
    <?php the_content(); ?>
    
    <?php endwhile; ?>
    

    #3 保持原样。

    希望这对其他人有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-15
      • 1970-01-01
      • 1970-01-01
      • 2016-03-06
      相关资源
      最近更新 更多