0

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

我还创建了一个单页模板,使用以下代码显示我的常见问题解答

$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(),并且我也尝试将它们删除但没有运气。

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

亲切的问候梅里亚诺斯尼科斯

4

1 回答 1

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");
于 2012-11-16T17:45:15.147 回答