首页 > 解决方案 > 如何为每个自定义分类法回显某些内容?

问题描述

我有一个页面,我想在其中显示各种智能手机的维修价格。我创建了自定义帖子类型“Reparaturpreise”(维修价格)以及自定义分类法“Marken”(品牌)。对于每个品牌,我想将品牌名称显示为标题,下面应该有一个表格,列出各种维修价格。

这是我到目前为止的代码:

<?php
               $args = array(
                           'taxonomy' => 'marke',
                           'orderby' => 'name',
                           'order'   => 'ASC'
                       );

               $cats = get_categories($args);

               foreach($cats as $cat) {
            ?>
            
            <h2><?php echo strip_tags(get_the_term_list( $post->ID, 'marke', '', ', ' ));?></h2>
                  
            <?php 
                    $args = array(
                        'post_type' => 'reparaturpreise',
                        'category'  => 'apple',
                        'posts_per_page' => 999
                    );
                    
                    $the_query = new WP_Query( $args ); 
                ?>
                
                    <table class="repair-prices-list">
                        <tr>
                            <th>Gerät</th>
                            <th>Display</th>
                            <th>Akku</th>
                            <th>Backcover</th>
                        </tr>

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

                        <tr>
                            <td><?php echo strip_tags(get_the_term_list( $post->ID, 'marke', '', ', ' ));?> <?php the_title(); ?></td>
                            <td>€ <?php echo get_post_meta($post->ID, 'displayreparatur', true); ?>,-</td>
                            <td>€ <?php echo get_post_meta($post->ID, 'akkutausch', true); ?>,-</td>
                            <td>€ <?php echo get_post_meta($post->ID, 'backcover-reparatur', true); ?>,-</td>
                        </tr>


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

                    </table>
            <?php
               }
            ?>

现在它显示了 3 个表格,但都具有相同的标题(第一个品牌),每个表格都显示了所有手机,而不是按品牌过滤。

您可以在此处查看当前输出: https ://relaunch.websolute.at/reparaturpreise/

提前感谢您的帮助,如果我的英语不是最好的,我们深表歉意。

标签: wordpresscustom-post-typecustom-taxonomy

解决方案


目前您有: <h2><?php echo strip_tags(get_the_term_list( $post->ID, 'marke', '', ', ' ));?></h2>

尝试: <h2><?php echo strip_tags(get_the_term_list( $post->ID, $cat, '', ', ' ));?></h2>

当您尝试获取不同的猫时。

您也可以更改posts_per_page-1相同的“无限”结果。


推荐阅读