簡體   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