【问题标题】:Category listing filtered for taxonomy term in WordPress在 WordPress 中为分类术语过滤的类别列表
【发布时间】:2023-03-30 19:01:01
【问题描述】:

我正在使用自定义帖子类型“Business”、分类法“Region”和“Categories”来指示业务类型。所以,例如业务:“妈妈咪呀比萨店”/地区:“鹿特丹”/类别:“比萨店”(父类别:“食品”)。

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

(1) 显示特定区域的所有类别(唯一列表 - 包括父级) - 仅在相关区域附加业务时显示类别。

(2) 显示特定区域的所有父类别(唯一列表) - 仅在相关区域的关联企业(通过子类别)时显示父类别。

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

我尝试了很多方法,但都没有成功。如果有人可以帮助我,我会非常高兴。

【问题讨论】:

  • 你介意发布一些你已经尝试过的sn-ps吗?

标签: wordpress


【解决方案1】:

广告 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。仅,如何过滤特定类别?

【讨论】:

    【解决方案2】:

    另外,我已将以下内容添加到我的 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-25
      • 1970-01-01
      • 2019-03-28
      • 2011-10-29
      • 2023-01-31
      • 1970-01-01
      相关资源
      最近更新 更多