解决Wordpress自定义分类archive页面无法通过分类罗列文章的问题
Wordpress有许多奇怪的逻辑,例如我创建一个叫产品的自定义分类,在产品总页面,无法通过自带函数根据产品分类的顺序罗列产品所在分类下的所有产品。
解决思路:
- 先根据get_categories获取所有产品分类。
- 根据所有产品分类foreach循环这个分类下的name、slug、term_id。
- 然后根据这些分类的id(tax_query)获取所有产品。
让我们开始吧。先定义2个数组,其中一个根据get_categories获取所有产品分类。
$test = get_categories("taxonomy=product&hide_empty=0");
$cats=array();
然后循环获取name、slug、term_id
foreach($test as $tmpcat){
if($tmpcat->parent==0){
$cats[$tmpcat->term_id]['name']=$tmpcat->name;
$cats[$tmpcat->term_id]['slug']=$tmpcat->slug;
$cats[$tmpcat->term_id]['term_id']=$tmpcat->term_id;
}
}
对数组排序
ksort($cats);
最后获取所有产品
<?php $i=0;
foreach($cats as $key=>$val){
$id = 'term_'.$cats[$key]['term_id'];
$taxId = (int)$cats[$key]['term_id'];
$i++; ?>
<h2>
<a href="/products/<?php echo $cats[$key]['slug']; ?>/">
<?php echo $cats[$key]['name']; ?>
</a>
</h2>
<div class="clearfix"></div>
<?php $args=array(
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'product',
'field' => 'term_id',
'terms' => $taxId,
)
)
);
$arms = array_merge($args, $wp_query->query);
query_posts($arms);
?>
<?php if (have_posts()) : ?>
<div class="all-product">
<?php while (have_posts()) : the_post(); ?>
<div class="col-md-4">
<div class="re-solution-product">
<div class="re-solution-product-imgcontent">
<a class="re-solution-product-img" href="<?php the_permalink(); ?>">
<img src="<?php echo get_field('product_img_cate')?>" alt="<?php the_title()?> images" />
</a>
</div>
<div class="re-solution-position">
<div class="re-solution-content">
<a class="re-solution-product-title" href="<?php the_permalink(); ?>"><?php the_title()?></a>
<ul>
<?php if( have_rows('product_adv') ):while ( have_rows('product_adv') ) : the_row();$adv = get_sub_field('product_adv');echo '<li>'.$adv.'</li>';endwhile;endif;?>
</ul>
<div class="re-products-content-a-link-s">
<a class="product-more" href="<?php the_permalink(); ?>">More Info <i class="fas fa-chevron-right"></i></a>
</div>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<div class="clearfix"></div>
<?php endif;
wp_reset_query();?>
<?php } ?>
以上是解决一种思路,如果有什么更好的思路可以一起探讨
Demo:
<div class="taxonomy-products we-all-product">
<?php
$test = get_categories("taxonomy=product&hide_empty=0");
$cats=array();
$cat_child=array();
foreach($test as $tmpcat){
if($tmpcat->parent==0){
$cats[$tmpcat->term_id]['name']=$tmpcat->name;
$cats[$tmpcat->term_id]['slug']=$tmpcat->slug;
$cats[$tmpcat->term_id]['term_id']=$tmpcat->term_id;
}
}
ksort($cats);
$i=0;
foreach($cats as $key=>$val){
$id = 'term_'.$cats[$key]['term_id'];
$taxId = (int)$cats[$key]['term_id'];
$i++;
?>
<h2>
<a href="/products/<?php echo $cats[$key]['slug']; ?>/">
<?php echo $cats[$key]['name']; ?>
</a>
</h2>
<div class="clearfix"></div>
<?php
$args=array(
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'product',
'field' => 'term_id',
'terms' => $taxId,
)
)
);
$arms = array_merge($args, $wp_query->query);
query_posts($arms);
?>
<?php if (have_posts()) : ?>
<div class="all-product">
<?php while (have_posts()) : the_post(); ?>
<div class="col-md-4">
<div class="re-solution-product">
<div class="re-solution-product-imgcontent">
<a class="re-solution-product-img" href="<?php the_permalink(); ?>">
<img src="<?php echo get_field('product_img_cate')?>" alt="<?php the_title()?> images" />
</a>
</div>
<div class="re-solution-position">
<div class="re-solution-content">
<a class="re-solution-product-title" href="<?php the_permalink(); ?>"><?php the_title()?></a>
<ul>
<?php if( have_rows('product_adv') ):while ( have_rows('product_adv') ) : the_row();$adv = get_sub_field('product_adv');echo '<li>'.$adv.'</li>';endwhile;endif;?>
</ul>
<div class="re-products-content-a-link-s">
<a class="product-more" href="<?php the_permalink(); ?>">More Info <i class="fas fa-chevron-right"></i></a>
</div>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<div class="clearfix"></div>
<?php endif;
wp_reset_query();?>
<?php } ?>
</div>
```