This link 告诉我如何显示分配给帖子的类别。
如何修改此代码以显示按层次顺序分配给帖子的自定义分类法中的术语?
This link 告诉我如何显示分配给帖子的类别。
如何修改此代码以显示按层次顺序分配给帖子的自定义分类法中的术语?
这取决于你到底想做什么。但是,如果希望链接的问题具有相同的功能,可以使用相同的功能,但要将自定义分类传递给该功能。这里是您可以传递给的所有参数wp_list_categories()
和is defulat值:
<?php $args = array(
\'show_option_all\' => \'\',
//Possible values of orderby: ID, name, slug, count, term_group
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'style\' => \'list\',
\'show_count\' => 0,
\'hide_empty\' => 1,
\'use_desc_for_title\' => 1,
\'child_of\' => 0,
\'feed\' => \'\',
\'feed_type\' => \'\',
\'feed_image\' => \'\',
\'exclude\' => \'\',
\'exclude_tree\' => \'\',
\'include\' => \'\',
\'hierarchical\' => 1,
\'title_li\' => __( \'Categories\' ),
\'show_option_none\' => __(\'No categories\'),
\'number\' => null,
\'echo\' => 1,
\'depth\' => 0,
\'current_category\' => 0,
\'pad_counts\' => 0,
//Change this with your custom taxonomy
\'taxonomy\' => \'your_custom_taxonomy\',
\'walker\' => null
);
wp_list_categories($args);
?>
上面的代码将以层次顺序生成一个列表,其中包含指定分类法中的所有术语,而不仅仅是与当前帖子关联的术语。但我们可以使用inlcude
参数:<?php
global $post;
$taxonomy = \'your_taxonomy\';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( \'fields\' => \'ids\' ) );
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( \',\' , $post_terms );
$args = array(
\'taxonomy\' => $taxonomy,
\'include\' => $term_ids,
//Add more arguments if you need them with a different value from default
);
$terms = wp_list_categories($args);
// display post categories
echo $terms;
}
?>
我有一个自定义的帖子类型,它设置了层次分类法。例如,我的帖子类型“project”的类别为 A A.1 B C 我正在尝试将分类显示为上的类<li> 每个帖子的项目,但我只想显示顶级家长。对于我正在查看的帖子,它被分类为A.1和C,但我想返回A和C。我正在使用\'parent\' => 0 然而,在args中,它给了我A.1和C。我也尝试使用\'hide_empty\' => 0 但这似乎没有帮助。这是我的代码: function pro