简体   繁体   English

Wordpress - 如何显示按自定义分类法分组的帖子?

[英]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 .这是我正在处理的页面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.当您遍历$cats数组时,您的查询不会改变。 Perhaps changing the the value of the 'terms' array to $cat->slug would give you better results.也许将 'terms' 数组的值更改为$cat->slug会给你更好的结果。

在您的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; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在wordpress循环中显示当前帖子的自定义分类名称? - How to display current posts custom taxonomy name inside wordpress loop? 在 Wordpress 中,如果一个分类法上有另一个分类法,我如何只显示它? - In Wordpress, how do I only display a taxonomy if it has another taxonomy on it? 如何在WordPress页面模板中显示分类分类的帖子? - How to display posts of taxonomy in WordPress page template? 如何在WordPress中通过自定义分类获取帖子? - How to get posts by custom taxonomy in WordPress? 如何在自定义Wordpress循环中隐藏过去的帖子并显示即将发布的X个帖子? - How do I hide past posts in a custom Wordpress loop AND display X number of upcoming posts? 按分类法对WordPress自定义帖子进行分组 - Grouping WordPress custom posts by taxonomy 如何在 WordPress 中将样式表排入队列以获取自定义分类档案? - How do I enqueue a stylesheet in WordPress for custom taxonomy archives? 如何将帖子链接到WordPress中的自定义“子分类法”? - How to link posts to a custom “sub-taxonomy” in WordPress? 如何根据 Wordpress 中的自定义分类过滤归档帖子 - How to filter archive posts based on custom taxonomy in Wordpress 我如何获得分类法类别,以便他们的帖子日期在wordpress中从最新到最旧 - How do i get taxonomy categories in order to their posts date in wordpress from latest to oldest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM