使用 WP_Term_Query 获取自定义分类法但仅显示来自 post 的分类法
Using WP_Term_Query to get Custom Taxonomies but show just the taxonomy from post
我正在使用此代码显示所有分类法:
'orderby' => 'ASC',
'post_type' => 'integrations_cpt',
'taxonomy' => 'categories_integration',
'hide_empty' => false,
);
$the_query = new WP_Query( $args );
$categories = new WP_Term_Query( $args );
<?php foreach ( $categories->terms as $category): ?>
<div class="row pl-3 p-1 sib-integrations-check fil-cat" data-rel="<?php echo $category->name?>">
<label class="sib-integrations-label"><?php echo $category->name?>
<input type="checkbox" class="chb"> <!-- hidden input -->
<span class="sib-integrations-checkmark" ></span>
</label>
</div>
并且在循环外工作正常。
但我现在想展示,在循环内部,只是来自 post 的分类法,而不是全部......
我试过的所有方法都不起作用...有人知道吗?
我的 CPT:
function create_integrations_custom_post_type()
{
register_post_type('integrations_cpt',
array(
'labels' => array(
'name' => __('Integrations'),
'singular_name' => __('Integration')
),
'public' => true,
'menu_icon' => 'dashicons-share',
'query_var' => true,
'has_archive' => true,
'taxonomies' => array('categories_integration'),
'supports' => array('custom-fields','title'),
)
);
register_taxonomy( 'categories_integration', 'integrations_cpt', array(
'hierarchical' => true,
'label' => 'Categories',
'query_var' => true,
'exclude_from_search' =>false,
'singular_label' => 'Category',
'has_archive' => true,
)
);
register_taxonomy_for_object_type( 'categories_integration', 'integrations_cpt' );
}
add_action('init', 'create_integrations_custom_post_type');
使用 get_terms() 函数查找所有类别。
$terms = get_terms('categories_integration');
foreach ($terms as $cat) {
echo $cat->name;
}
我正在使用此代码显示所有分类法:
'orderby' => 'ASC',
'post_type' => 'integrations_cpt',
'taxonomy' => 'categories_integration',
'hide_empty' => false,
);
$the_query = new WP_Query( $args );
$categories = new WP_Term_Query( $args );
<?php foreach ( $categories->terms as $category): ?>
<div class="row pl-3 p-1 sib-integrations-check fil-cat" data-rel="<?php echo $category->name?>">
<label class="sib-integrations-label"><?php echo $category->name?>
<input type="checkbox" class="chb"> <!-- hidden input -->
<span class="sib-integrations-checkmark" ></span>
</label>
</div>
并且在循环外工作正常。 但我现在想展示,在循环内部,只是来自 post 的分类法,而不是全部...... 我试过的所有方法都不起作用...有人知道吗?
我的 CPT:
function create_integrations_custom_post_type()
{
register_post_type('integrations_cpt',
array(
'labels' => array(
'name' => __('Integrations'),
'singular_name' => __('Integration')
),
'public' => true,
'menu_icon' => 'dashicons-share',
'query_var' => true,
'has_archive' => true,
'taxonomies' => array('categories_integration'),
'supports' => array('custom-fields','title'),
)
);
register_taxonomy( 'categories_integration', 'integrations_cpt', array(
'hierarchical' => true,
'label' => 'Categories',
'query_var' => true,
'exclude_from_search' =>false,
'singular_label' => 'Category',
'has_archive' => true,
)
);
register_taxonomy_for_object_type( 'categories_integration', 'integrations_cpt' );
}
add_action('init', 'create_integrations_custom_post_type');
使用 get_terms() 函数查找所有类别。
$terms = get_terms('categories_integration');
foreach ($terms as $cat) {
echo $cat->name;
}