wp_query在无限循环中

时间:2012-04-07 14:54:28

标签: php wordpress

我正在尝试做这个循环,但它是无限的

我只需要在<li>代码上显示一些结果。我正在编辑loop.php来执行此操作,而函数if(function_exists('wp_custom_fields_search')) wp_custom_fields_search();只返回我创建的一个插件的形式。

你能帮帮我吗?

<?php
        $queryA = new WP_Query($args1);
        $queryB = new WP_Query($args2);

        $args1 = array (
        'orderby' => 'title', 
        'order' => 'ASC', 
        'category_name' => 'lojas',
        'posts_per_page' => '-1',
        );

        $args2 = array (
        'orderby' => 'title', 
        'order' => 'ASC', 
        'category_name' => 'gastronomia',
        'posts_per_page' => '-1',
        );  


        if ($queryA->have_posts()) {
            if(function_exists('wp_custom_fields_search')) wp_custom_fields_search();
            while($queryA->have_posts()) : $queryA->the_post();
                echo '<li>';
                echo '<span class="nome">';
                the_title();
                echo '</span>';
                echo '<span class="end">';
                echo get('endereco');
                echo '</span>';
                echo '<span class="tel">';
                echo get('telefone');
                echo '</span>';
                echo '</li>';
            endwhile;
        } 


        if ($queryB->have_posts()) {
            if(function_exists('wp_custom_fields_search')) wp_custom_fields_search('preset-1');
            while($queryB->have_posts()) : $queryB->the_post();
                echo '<li>';
                echo '<span class="nome">';
                the_title();
                echo '</span>';
                echo '<span class="end">';
                echo get('endereco');
                echo '</span>';
                echo '<span class="tel">';
                echo get('telefone');
                echo '</span>';
                echo '</li>';
            endwhile;
        }       


    ?>

1 个答案:

答案 0 :(得分:0)

我必须承认,从您的代码中我没有看到可能导致无限循环的第一手资料,但是您可以大幅减少代码,这可能有助于您找到错误:

$defaultArgs = array (
    'orderby' => 'title', 
    'order' => 'ASC', 
    'posts_per_page' => '-1',
);
$queries = array(
    array(array('category_name' => 'lojas'), NULL),
    array(array('category_name' => 'gastronomia'), 'preset-1'),
);    
$customFiledsSearch = function_exists('wp_custom_fields_search');
foreach ($queries as $query)
{
    list($args, $param) = $query;
    $wpQuery = new WP_Query($args + $defaultArgs);
    if ($wpQuery->have_posts()) 
    {
        if ($customFiledsSearch)
        {
            wp_custom_fields_search($param);
        }
        while ($wpQuery->have_posts()) : $wpQuery->the_post())
        {
            echo '<li>', 
                   '<span class="nome">',
                     the_title();
                   '</span>',
                   '<span class="end">', 
                     get('endereco'), 
                   '</span>', 
                   '<span class="tel">', 
                     get('telefone'), 
                   '</span>', 
                 '</li>';

        }
    }
}

也许这可以帮助您找到错误。

相关问题