问题描述
我有一个页面,我想显示各种智能手机的维修价格。 我创建了自定义帖子类型“ 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,' ));?> <?PHP the_title(); ?></td>
<td>€ <?PHP echo get_post_meta($post->ID,'displayreparatur',true); ?>,-</td>
<td>€ <?PHP echo get_post_meta($post->ID,'akkutausch','backcover-reparatur',-</td>
</tr>
<?PHP
wp_reset_postdata();
endwhile;
endif;
?>
</table>
<?PHP
}
?>
现在它显示了3个表格,但所有表格都具有相同的标题(第一品牌),并且每个表格都显示了所有手机,未按品牌进行过滤。
您可以在此处查看当前输出: https://relaunch.websolute.at/reparaturpreise/
在此先感谢您的帮助,如果我的英语不是最好的话,请对不起。
解决方法
当前,您有:
<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
,以获得相同的“无限”结果。
我设法自己解决了,这是我使用的代码:
<?php
$custom_terms = get_terms('marke');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'reparaturpreise','tax_query' => array(
array(
'taxonomy' => 'marke','field' => 'slug','terms' => $custom_term->slug,),);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>'; ?>
<table class="repair-prices-list">
<tr>
<th>Gerät</th>
<th>Display</th>
<th>Akku</th>
<th>Backcover</th>
</tr>
<?php
while($loop->have_posts()) : $loop->the_post(); ?>
<tr>
<td><?php echo strip_tags(get_the_term_list( $post->ID,' ));?> <?php the_title(); ?></td>
<td>€ <?php echo get_post_meta($post->ID,'displayreparatur',true); ?>,-</td>
<td>€ <?php echo get_post_meta($post->ID,'akkutausch','backcover-reparatur',-</td>
</tr>
<?php
endwhile;
?>
</table>
<?php
}
}
?>