Wordpress 特定职位类型的分类列表

Wordpress 特定职位类型的分类列表,wordpress,custom-taxonomy,Wordpress,Custom Taxonomy,如何使用自定义post类型名称获取自定义分类列表。 假设有一种称为“产品”的帖子类型,其中有一个类别列表(即衬衫、T恤、牛仔裤等) 所以我想要使用post类型名称“products”的类别列表。您可以通过taxonomy-{post_type}.php中的taxonomy获得post列表,默认情况下,该文件中的gt list of post用于指定的分类法 参考: 或者在定制模板中 $custom_terms = get_terms('custom_taxonomy'); foreach($c

如何使用自定义post类型名称获取自定义分类列表。 假设有一种称为“产品”的帖子类型,其中有一个类别列表(即衬衫、T恤、牛仔裤等)


所以我想要使用post类型名称“products”的类别列表。

您可以通过taxonomy-{post_type}.php中的taxonomy获得post列表,默认情况下,该文件中的gt list of post用于指定的分类法

参考:

或者在定制模板中

$custom_terms = get_terms('custom_taxonomy');

foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array('post_type' => 'custom_post_type',
        'tax_query' => array(
            array(
                'taxonomy' => 'custom_taxonomy',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );

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

        while($loop->have_posts()) : $loop->the_post();
            echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
        endwhile;
     }
}
$custom_terms=get_terms('custom_taxonomy');
foreach($custom\u terms作为$custom\u term){
wp_reset_query();
$args=array('post\u type'=>'custom\u post\u type',
“tax_query”=>数组(
排列(
“分类法”=>“自定义分类法”,
'字段'=>'段塞',
“术语”=>$custom\u term->slug,
),
),
);
$loop=新的WP_查询($args);
如果($loop->have_posts()){
回显“.$custom_term->name.”;
而($loop->have_posts()):$loop->the_post();
回声“
”; 结束时; } }
您可以使用以下内容:

$terms = get_terms( 'products' );
$count = count( $terms );
if ( $count > 0 ) {
  echo '<h3>Total products: '. $count . '</h3>';
  echo '<ul>';
  foreach ( $terms as $term ) {
    echo '<li><a href="'.get_term_link($term).'">'.$term->name.'</a></li>';
  }
  echo '</ul>';
}
$terms=get_terms('products');
$count=count($terms);
如果($count>0){
echo“产品总数:”.$count.”;
回声“
    ”; foreach($terms作为$term){ 回音“
  • ”; } 回声“
”; }

get\u term\u link()也将为您提供一个指向术语的链接。

谢谢您的回复,但我需要在特定模板中使用一些查询。这是基于分类名称的,但我需要基于帖子类型名称的类别列表。在循环中提到$args=array('post_type'=>'custom_post_type',试试这个,但是这里你需要添加你的自定义分类法名称,我不想添加任何地方的分类法名称。$custom_terms=get_terms('custom_taxonomy');使用get_terms(),它会给出所有分类法的列表,然后在循环中你可以过滤自定义的post类型