繁体   English   中英

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

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

我正在使用自定义帖子类型和自定义分类法创建一个常见问题页面。 我正在尝试为每个分类法创建一个无序列表,以便对常见问题进行分组。 在该无序列表中,我希望列出的第一个项目是分类法名称,然后针对分类法中的所有问题重复列出的第二个项目。 这是我正在处理的页面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 ?>

当您遍历$cats数组时,您的查询不会改变。 也许将 'terms' 数组的值更改为$cat->slug会给你更好的结果。

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

非常感谢。 你们都提供了关于我所缺少的东西的深刻见解。 我现在已经解决了,这就是我在考虑您的建议的情况下解决它的方法。

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

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