【发布时间】:2015-03-31 10:42:30
【问题描述】:
我目前有一个“产品”的自定义帖子类型。
如您所见,我现在拥有由以下代码创建的外部和内部产品:
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,
)
);
}
我想要实现的是一个页面,它只能显示外部产品中的类别。
我有这个 sn-p 代码,它同时显示外部和内部:
<?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>';
}
?>
【问题讨论】:
-
也许您可以使用 'child_of' 参数并传递所需分类的 ID 来仅获取外部或内部术语?
-
请使用模板层次结构。您可以在其中管理来自特定分类的所有产品。
-
我会试试那个trainoasis。对不起,我不关注 Riteshdjoshi
标签: php wordpress content-management-system custom-post-type custom-taxonomy