如何在 WordPress 中检索自定义 post 类型的分类键?

How do I retrieve a custom post type taxonomy key in WordPress?

我在 archive.php 页面上尝试检索自定义 post 类型分类法(键?)。

我的站点有 4 个自定义 post 类型,它们都将使用 archive.php 来显示 posts:

即。 http://localhost/thebridestree/vendor_category/beauty-and-makeup-3/ http://localhost/thebridestree/pinboard_category/accessories/

在 archive.php 页面上,我需要能够检索分类法(键?):

即。 vendor_category 或 pinboard_category.

从这里,我可以显示 posts 查询:

$the_query = new WP_Query(array(
                'post_type'         => 'vendor',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'vendor_category',
                        'field'    => 'slug',
                        'terms'    => $catslug,

我在这个问题上搜索了几个小时 - 大多数答案都与检索类别有关,即。美容化妆 3

试试这个 get_query_var('taxonomy') 将 return 当前分类和 get_query_var( 'term' ) 将 return 当前 term / category 创建单独的数组。最后获取所有与您的条件相关的帖子。

例如:

post type = 'vendor' , taxonomy = 'vendor_category'term ='beauty-and-makeup-3'.

<?php 
    $taxonmy    = get_query_var('taxonomy'); 
    $term       = get_query_var( 'term' );
    $tax_array  = array(
                'taxonomy' => $taxonmy,
                'field' => 'slug',
                'terms' => $term
                );

    $args = array(
                'post_type'  => 'vendor',
                'nopaging'   => true,
                'tax_query'  => array(
                                  'relation' => 'AND',
                                  $tax_array
                               )
               );
    $postsobject = new WP_Query($args);

    while ($postsobject->have_posts()): $postsobject->the_post();

           ?>
           <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
           <?php 

    endwhile;
?>