在WordPress中过滤分类术语的类别列表

时间:2010-11-24 13:20:28

标签: wordpress

我使用自定义帖子类型“商家”,分类“区域”和“类别”来表示商家类型。所以,例如经营范围:“Mamma Mia Pizzeria”/地区:“鹿特丹”/类别:“披萨店”(父母类别:“食品”)。

现在,我想创建一些列表:

(1)显示特定区域的所有类别(唯一列表 - 包括父级) - 如果有关联的业务,则仅显示相关区域的类别。

(2)显示特定区域的所有父类别(唯一列表) - 如果有相关区域的业务(通过子类别),则仅显示父类别。

(3)显示属于特定类别和特定区域的业务(详细信息)列表

我尝试过很多东西,但没有成功。如果有人能帮助我,我会非常高兴。

2 个答案:

答案 0 :(得分:0)

广告1)我尝试了什么:

// show a list of categories for a region (taxonomy = "region")

<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>

<?php query_posts(array( 'post_type'=>'business', 'region'=>$term->slug)); ?>

<?php
if (have_posts()) :
   while (have_posts()) :
      the_post();
      the_category(' &raquo; ','multiple');
      echo '<br/>';
   endwhile;
endif;
?>

// but, how to show a category only once?

广告2)我尝试过的内容:请参阅广告1)以及如何仅显示父类别?

广告3)我尝试过的内容:请参阅广告1.仅限,如何过滤特定类别?

答案 1 :(得分:0)

另外,我已将以下内容添加到我的functions.php中:

// Add Business to the query

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if(is_category() || is_tag()) {
    $post_type = get_query_var('post_type');
    if($post_type)
        $post_type = $post_type;
    else
        $post_type = array('post','business','nav_menu_item');
    $query->set('post_type',$post_type);
    return $query;
    }
}
相关问题