[英]Display taxonomy from custom post type
我目前具有“产品”的自定义帖子类型。
如您所见,我现在拥有由以下代码创建的外部和内部产品:
add_action( 'init', 'create_product__cat_external' );
function create_product__cat_external() {
register_taxonomy(
'ExternalProducts',
'products',
array(
'label' => __( 'External Products' ),
'rewrite' => array( 'slug' => 'externalproducts' ),
'hierarchical' => true,
)
);
}
add_action( 'init', 'create_product__cat_internal' );
function create_product__cat_internal() {
register_taxonomy(
'InternalProducts',
'products',
array(
'label' => __( 'Internal Products' ),
'rewrite' => array( 'slug' => 'internalproducts' ),
'hierarchical' => true,
)
);
}
我要达到的目的是,一个页面只能显示“外部产品”中的类别。
我有以下代码片段,显示了外部和内部:
<?php
$customPostTaxonomies = get_object_taxonomies('products');
if(count($customPostTaxonomies) > 0)
{
foreach($customPostTaxonomies as $tax)
{
$args = array(
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 1,
'taxonomy' => $tax,
'title_li' => ''
);
wp_list_categories( $args );
}
}
?>
任何帮助都会很棒。 干杯。
更新:
我目前拥有该类别的名称,并输出描述,但是该链接未链接以显示该类别内的帖子。
代码如下:
<?php
$taxonomy = 'ExternalProducts';
$queried_term = get_query_var($taxonomy);
$terms = get_terms($taxonomy, 'slug='.$queried_term);
if ($terms) {
echo '<ul>';
foreach($terms as $term) {
// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
continue;
}
// We successfully got a link. Print it out.
echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
echo $term->description;
}
echo '</ul>';
}
?>
您需要使用函数“ get_terms”。这是简短的代码。 假设您的分类法名称= custom_taxonomy
$catlsit = get_terms('custom_taxonomy', array( 'orderby' => 'count', 'hide_empty' => 0 ) ); print_r($catlsit);
您将获得在分类法:custom_taxonomy中创建的类别列表。 如果您需要其他任何东西,请写下来。 谢谢,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.