首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >显示所有按自定义分类法排序的自定义post类型文章,然后按另一个自定义分类法排序。

显示所有按自定义分类法排序的自定义post类型文章,然后按另一个自定义分类法排序。
EN

WordPress Development用户
提问于 2018-03-08 08:36:27
回答 1查看 1.2K关注 0票数 1

抱歉把标题弄乱了。不知道如何更好地描述它。我想展示所有的商店,首先按他们的分类,然后,在里面,按他们的位置排序。

我有自定义邮政类型的“商店”,两个定制分类“商店类别”,“商店的位置”。

例如,我希望它能显示:

London

  • 01店
  • 02铺

东京

  • 05店
  • 06铺

电子

London

  • 第11铺
  • 第12铺

东京

  • 15铺
  • 第16铺

鞋,电子学,(.)-分类学#1

伦敦,东京,(.)-分类学#2

01店,(.)-自定义邮政类型

到目前为止还能用一个分类法来显示。虽然效果不错,但我还需要一个水平。

代码语言:javascript
复制
 'shop',
    'tax_query' => array(
        array(
            'taxonomy' => 'shop_category',
            'field' => 'slug',
            'terms' => $custom_term->slug,
        ),
    ),
);

$loop = new WP_Query($args);
if($loop->have_posts()) {
    echo ''.$custom_term->name.'';

    while($loop->have_posts()) : $loop->the_post();
        echo ''.get_the_title().'
';
    endwhile;
}

}
?>

提前谢谢你的指示。

EN

回答 1

WordPress Development用户

回答已采纳

发布于 2018-03-08 09:01:22

首先要有一个类别列表和一个位置列表。然后循环遍历每个类别。在每个类别循环中,遍历具有当前类别和当前位置的位置列表和查询帖子,并输出列表:

代码语言:javascript
复制
$categories = get_terms( array(
    'taxonomy' => 'shop_category',
) );

$locations = get_terms( array(
    'taxonomy' => 'shop_location',
) );

foreach ( $categories as $category ) {
    echo '' . $category->name . '';

    foreach ( $locations as $location ) {
        $shops = new WP_Query( array(
            'post_type' => 'shop',
            'tax_query' => array(
                array(
                    'terms'    => $category->term_id,
                    'taxonomy' => 'shop_category',
                ),
                array(
                    'terms'    => $location->term_id,
                    'taxonomy' => 'shop_location',
                ),
            ),
        ) );

        if( $shops->have_posts() ) {
            echo '' . $location->name . '';

            while ( $shops->have_posts() ) : $shops->the_post();
                echo '' . get_the_title() . '
';
            endwhile;
        }

        wp_reset_postdata();
    }
}
票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/296176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档