WordPress - 如何按自定义分类显示帖子分组?

3
我正在使用自定义文章类型和自定义分类法创建一个FAQ页面。我试图为每个分类法创建一个无序列表,以便对FAQ进行分组。在该无序列表中,我希望第一个列出的项目是分类法名称,然后重复第二个列出的项目,以用于分类法中的所有问题。这是我正在处理的页面 link
目前它正在复制帖子,而不是按照正确的分类法显示。
                        <?php
                        // get all the categories from the database
                        $cats = get_terms( array(
                            'taxonomy' => 'faq_categories',
                        )); 

                        // loop through the categories
                        foreach ($cats as $cat) {
                            // setup the category ID
                            $cat_id = $cat->term_id;
                        ?>

                                <!-- Make a header for the category -->
                        <ul id="<?php echo $cat->slug; ?>" class="cd-faq-group">
                            <li class="cd-faq-title">
                                <h2>Questions <?php echo $cat->name; ?></h2>
                            </li>

                            <?php

                            // create a custom wordpress query

                            query_posts( array(
                                'post_type' => 'faqs',
                                'tax_query' => array( 
                                    array( 
                                        'taxonomy' => 'faq_categories', //or tag or custom taxonomy
                                        'field' => 'slug', 
                                        'terms' => 'for-women'
                                    ) 
                                ) 
                            ));

                            // start the wordpress loop!
                            if (have_posts()) : while (have_posts()) : the_post(); ?>

                            <li>
                                <a class="cd-faq-trigger" href="#0"><?php the_title(); ?></a>
                                <div class="cd-faq-content">
                                    <?php the_content(); ?>
                                </div> 
                            </li>

                            <?php endwhile; endif; // done our wordpress loop. Will start again for each category 

                            wp_reset_postdata();
                            ?>


                        </ul>
                        <?php } // done the foreach statement ?>
3个回答

2
非常感谢你们,你们提供了很好的见解,帮助我找到了问题所在。我已经解决了这个问题,并且考虑了你们的建议。以下是我的解决方案。
<?php

    $cats = get_terms( 
        array(
            'taxonomy' => 'faq_categories',
            'orderby' => 'term_id',
            'order' => 'ASC'
        )
    ); 

    foreach ($cats as $cat) :
?>


<ul id="<?php echo $cat->slug; ?>" class="cd-faq-group">
    <li class="cd-faq-title">
        <h2>Questions <?php echo $cat->name; ?></h2>
    </li>
<?php

    $questions = new WP_Query(
        array(
            'category_name' => $cat->slug
        )
    );

    $questions = new WP_Query( array(
        'post_type' => 'faqs',
        'order' => 'ASC',
        'tax_query' => array( 
            array( 
                'taxonomy' => 'faq_categories',
                'field' => 'slug', 
                'terms' => array($cat->slug),
            )
        ) 
    ));
?>

<?php if ($questions->have_posts()) :  while ($questions->have_posts()) : $questions->the_post();?>

    <li>
        <a class="cd-faq-trigger" href="#0"><?php the_title(); ?></a>
        <div class="cd-faq-content">
            <?php the_content(); ?>
        </div>
    </li>

    <?php endwhile; ?>

    <?php wp_reset_postdata(); ?>
<?php endif; ?>

2

当您遍历$cats数组时,您的查询不会发生变化。也许将“terms”数组的值更改为$cat->slug会给您带来更好的结果。


0
在您的 query_post 中,tax_queryfield 应该为 term_id,而且 terms 应该分配给您的 $cat_id 变量,而不是硬编码术语。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接