简体   繁体   中英

Wordpress - How do I display posts grouped by custom taxonomy?

I am creating a FAQ page using a custom post type and custom taxonomy. I'm trying to create an unordered list for each taxonomy in order to group the FAQ's. In that unordered list, I want the first listed item to be the taxonomy name and then repeat the second listed item for all the questions in the taxonomy. This is the page I'm working on link .

It's currently duplicating the posts instead of displaying in the rightful taxonomies.

                        <?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 ?>

Your query is not changing as you iterate through the $cats array. Perhaps changing the the value of the 'terms' array to $cat->slug would give you better results.

在您的query_post ,您的tax_queryfield应该是term_id并且您的terms被分配给您的$cat_id变量,而不是一个硬编码的条款。

Thank you so much. You have both provided great insight as to what I was missing. I've resolved it now and this is how I went about solving it taking your suggestions into consideration.

<?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; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM