WordPress术语循环使用foreach和WP_Qurey作为嵌套循环问题

时间:2012-11-16 17:00:54

标签: wordpress

我为常见问题解答和自定义分类法创建了一个自定义帖子类型来整理问题和解答。答案。

我还创建了一个单页模板,通过使用

之后的代码显示我的常见问题解答
$terms = get_terms(
    'faq_categories',
    array(
        'orderby'   =>  'name',
        'order'     =>  'ASC'
    )
);

foreach($terms as $term)
{
    ?>
    <h3><?php echo $term->name; ?></h3>
    <?php

    $q_args = array(
        'post_type'         =>  'faq',
        'tax_query'         =>  array(
            'taxonomy'  =>  'faq_categories',
            'field'     =>  'slug',
            'terms'     =>  $term->slug
        ),
        'posts_per_page'    =>  -1
    );

    wp_reset_postdata();
    wp_reset_query();

    $ans    =   new WP_Query($q_args);

    while($ans->have_posts())
    {
        $ans->the_post();

        ?>
        <h5><?php echo the_title(); ?></h5>
        <?php
    }
}

我的问题是,当我收到问题标题时,问题不按常见问题类别分组,在每个类别下,我会重复提供所有可用的问题。

结果如下:

Sale [FAQ Category]
    How to buy? [FAQ Question]
    What is the cost? [FAQ Question]
    How can I contact you? [FAQ Question]
    What is your address? [FAQ Question]
Contacts [FAQ Category]
    How to buy? [FAQ Question]
    What is the cost? [FAQ Question]
    How can I contact you? [FAQ Question]
    What is your address? [FAQ Question]

此外,我在WP_Query循环之前和之后尝试使用 wp_reset_postdate() wp_reset_query(),我也尝试删除它们而没有运气。

关于如何解决这个问题的任何想法?

亲切的问候 Merianos Nikos

1 个答案:

答案 0 :(得分:1)

tax_query接受数组数组。

$q_args = array(
    'post_type'         =>  'faq',
    'tax_query'         =>  array(
        array(
            'taxonomy'  =>  'faq_categories',
            'field'     =>  'slug',
            'terms'     =>  $term->slug
        )
    ),
    'posts_per_page'    =>  -1
);

或者重写你的查询,你真的不需要tax_query:

$ans = new WP_Query("post_type=faq&faq_categories=$term->slug&posts_per_page=-1");