首页 > 解决方案 > Wordpress 在页面上显示匹配分类的过滤列表

问题描述

我有三个相关的分类tax1 = make和tax2 = body和tax3 = make-body

tax3 是前两者的组合

我想显示从“make”存档页面到所有有帖子的“make-body”存档页面的链接(不显示指向空存档页面的链接)

然后我想在“正文”存档页面上做相反的事情。

所以我需要部分匹配'给我展示一个不为空的'make-body' slug 数组并匹配当前存档页面的'body'或'make'

如果我可以在链接之后计算可用的“make-body”数量,那么蛋糕上的奶油就可以了。我不是 php 方面的专家(轻描淡写),但我可以看到这会返回一个“make-body”slug 列表和当前存档页面的 slug。

努力过滤“make-body”结果以仅显示与页面相关的结果。

我假设我必须内爆数组并将 URL 的其余部分添加到 slug...

感谢您在这里的任何帮助(这是第一次发帖,所以我确信我已经践踏了所有礼仪!)

到目前为止,我有以下构建数组(借自另一个 SO 问题)

$terms = get_the_terms( $post->ID , 'body' );
    foreach ( $terms as $term ) {
    }   
    $filter =($term->slug);
    $termArgs = array(
    'post_status' => 'publish',
    'orderby' => 'name',
    'order' => 'ASC',
    'post_type' => 'page',
    'tax_query' => array(
    'relation' => 'AND',
    array(
    'taxonomy' => 'make-body',
    'field'    => 'term_id',
    'terms'    => get_terms( 'make-body', array( 'hide_empty' => true, 'fields' => 'slugs')),
    'operator' => 'IN',
    ),
    array(
    'taxonomy' => 'body',
    'terms'    => $filter,
    'field'    => 'slug'
    )
    )
    );
    print_r($termArgs);

标签: phpwordpressfiltertaxonomy

解决方案


基于匹配分类法的第一部分,我设法让它发挥作用。现在,这为我提供了以父分类术语开头的所有术语,并带有指向存档页面的链接。

$categories = array();
$names = array(); 
$reg = '/^'. ($term->slug) .'/';
    $category_list_items = get_terms( 'your-taxonomy', array( 'hide_empty' => true, ));
    foreach($category_list_items as $category_list_item){
    if(! empty($category_list_item->slug) ){
        $categories[] = $category_list_item->slug;
        $names[] = $category_list_item->name;
    }
}
foreach ($categories as $key=>$category) {
     if(preg_match($reg,$category)){
     echo "<a  href=/absolute-path/".$category ."/>"; 
    echo $names[$key] . " items</a> |";
    $args = array( 'post_type' => 'listing', 'posts_per_page' => 100, 'your-taxonomy' => $category, 'orderby' => 'rand' );
}
}


推荐阅读