2016-08-15 61 views
0

我环顾四周寻找解释我的问题的信息,但没有找到。我有一个循环,据说应该列出所有“人员”的儿童类别。由于某些原因,循环会无限重启。我已经检查过,它从foreach和while循环中结束,但仍然保持循环。有任何想法吗?为什么query_posts在这里造成无限循环?

我在这里调用该函数:

// Personnel listing 
upp_loop('Personal', 'personnel-preview', '<section class="res-table">', '</section>'); 

这是代码生成帖子:

// Loop through children (To make headers) 
       $cats = get_categories('child_of=' . $catID . '&orderby=count&order=DESC'); 

       foreach ($cats as $cat) : 

       $args = array(
        'cat' => $cat->term_id 
       ); 
       $query = new WP_query($args); 

        if ($query->have_posts()) : 
         // Echo the Category name 
         ?> <h2><?php echo $cat->name; ?></h2> <?php 

         // List all children 
         while($query->have_posts()) : 
          $query->the_post(); 
           if (has_post_thumbnail()) { 
            $img = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'single-post-thumbnail'); 
           } 
           else { 
            $img[0] = get_template_directory_uri() . "/img/program-default.png"; 
           }?> 
            <a href="<?php the_permalink(); ?>" class="res-td"> 
             <img src="<?php echo $img[0] ?>" alt="<?php the_title(); ?>"/> 
             <h2><?php the_title() ?></h2> 
            </a> 
           <?php 
         endwhile; 
        endif; 
       endforeach; 
+0

请看文档如何[查询职位(http://stackoverflow.com/documentation/wordpress/ 4002 /查询-职位#吨= 201608150832028118879)。从来没有使用'query_posts'这个... –

+0

@dingo_d我做了一个新的WP_query对象,现在它不会卡住无限。然而,它做了4次foreach。任何想法为什么? –

+0

那么如果你有4个类别,那么它只会做'foreach'四次。 –

回答

0

您需要定义您的查询的变量,然后调用它的方法。

使这些变化在你的代码: query_posts($args)$query = query_posts($args) have_posts()$query->have_posts() the_post()$query->the_post()

+0

获取错误:致命错误:调用成员函数have_posts() –