admin管理员组文章数量:1023827
If anyone know, if there's plugin to customize taxonomy list it will be faster to do.
But here's my question.
i create post type "product" and the categories.
inside post type product, i make categories like this
- Book
- Novel
- Dictionary
- English
- Korean
- Gadget
- Phone
- Tablet
So, in the page
i want to list sub-category inside Book,
the result = Novel and Dictionary.
And when i click on Dictionary for example, it will show me
list of Dictionary sub-category,
the result = English and Korean.
Then for last, when i click on English
page will show all post items inside English category.
If anyone know, if there's plugin to customize taxonomy list it will be faster to do.
But here's my question.
i create post type "product" and the categories.
inside post type product, i make categories like this
- Book
- Novel
- Dictionary
- English
- Korean
- Gadget
- Phone
- Tablet
So, in the page
i want to list sub-category inside Book,
the result = Novel and Dictionary.
And when i click on Dictionary for example, it will show me
list of Dictionary sub-category,
the result = English and Korean.
Then for last, when i click on English
page will show all post items inside English category.
- This should help: How to list custom taxonomy categories? – nmr Commented May 2, 2019 at 10:49
- i try to list, but when i put the code it show me all list of categories. what i mean is, when i'm on Book category page, the page will showing Novel and dictionary only. – Brian Commented May 2, 2019 at 11:03
3 Answers
Reset to default 0To limit the results to the children of the current product category, you can use parent
parameter in get_terms(). The id
of the current category, to be set as the parent
value, will be get using get_queried_object().
$term_id = 0;
$queried_object = get_queried_object();
//
// check if it's a custom category
if ( $queried_object instanceof WP_Term )
$term_id = $queried_object->term_id;
$args = array(
'taxonomy' => 'product_cat',
'parent' => $term_id, // get only direct children
//'child_of' => $term_id, // get all children
//'hide_empty' => false,
);
$terms = get_terms( $args );
If you would like to attach the current category to the results (before the child categories):
$terms = get_terms( $args );
if ($queried_object instanceof WP_Term) {
array_unshift( $terms, $queried_object );
}
An example of the code in its entirety:
if ( is_archive('product') ) // OR: is_product_category()
{
$term_id = 0;
$queried_object = get_queried_object();
//
// check if it's a custom category
if ( $queried_object instanceof WP_Term )
$term_id = $queried_object->term_id;
$args = array(
'taxonomy' => 'product_cat',
'parent' => $term_id, // get only direct children
//'child_of' => $term_id, // get all children
//'hide_empty' => false,
);
$terms = get_terms( $args );
//
// add current category at beginning
if ($queried_object instanceof WP_Term) {
array_unshift( $terms, $queried_object );
}
//
// Check if any term exists and print them all
if ( ! empty( $terms ) && is_array( $terms ) )
{
echo '<ul>';
foreach ( $terms as $term ) {
printf('<li><a href="%s">%s</a></li>',
esc_url( get_term_link( $term ) ),
$term->name
);
}
echo '</ul>';
}
}
<?php
$args = array(
'taxonomy' => 'your-category-name',
'hierarchical' => true,
'title_li' => '',
'hide_empty' => false
);
?>
<ul>
<?php wp_list_categories( $args ); ?>
</ul>
You can use the wp_list_categories function also for taxonomies.
http://codex.wordpress/Template_Tags/wp_list_categories
I found this, to be more specific i put parent category id for it.
and maybe i will make the result as shortcode.
$term_id = 3;
$taxonomy_name = 'category_name';
$termchildren = get_term_children( $term_id, $taxonomy_name );
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
If anyone know, if there's plugin to customize taxonomy list it will be faster to do.
But here's my question.
i create post type "product" and the categories.
inside post type product, i make categories like this
- Book
- Novel
- Dictionary
- English
- Korean
- Gadget
- Phone
- Tablet
So, in the page
i want to list sub-category inside Book,
the result = Novel and Dictionary.
And when i click on Dictionary for example, it will show me
list of Dictionary sub-category,
the result = English and Korean.
Then for last, when i click on English
page will show all post items inside English category.
If anyone know, if there's plugin to customize taxonomy list it will be faster to do.
But here's my question.
i create post type "product" and the categories.
inside post type product, i make categories like this
- Book
- Novel
- Dictionary
- English
- Korean
- Gadget
- Phone
- Tablet
So, in the page
i want to list sub-category inside Book,
the result = Novel and Dictionary.
And when i click on Dictionary for example, it will show me
list of Dictionary sub-category,
the result = English and Korean.
Then for last, when i click on English
page will show all post items inside English category.
- This should help: How to list custom taxonomy categories? – nmr Commented May 2, 2019 at 10:49
- i try to list, but when i put the code it show me all list of categories. what i mean is, when i'm on Book category page, the page will showing Novel and dictionary only. – Brian Commented May 2, 2019 at 11:03
3 Answers
Reset to default 0To limit the results to the children of the current product category, you can use parent
parameter in get_terms(). The id
of the current category, to be set as the parent
value, will be get using get_queried_object().
$term_id = 0;
$queried_object = get_queried_object();
//
// check if it's a custom category
if ( $queried_object instanceof WP_Term )
$term_id = $queried_object->term_id;
$args = array(
'taxonomy' => 'product_cat',
'parent' => $term_id, // get only direct children
//'child_of' => $term_id, // get all children
//'hide_empty' => false,
);
$terms = get_terms( $args );
If you would like to attach the current category to the results (before the child categories):
$terms = get_terms( $args );
if ($queried_object instanceof WP_Term) {
array_unshift( $terms, $queried_object );
}
An example of the code in its entirety:
if ( is_archive('product') ) // OR: is_product_category()
{
$term_id = 0;
$queried_object = get_queried_object();
//
// check if it's a custom category
if ( $queried_object instanceof WP_Term )
$term_id = $queried_object->term_id;
$args = array(
'taxonomy' => 'product_cat',
'parent' => $term_id, // get only direct children
//'child_of' => $term_id, // get all children
//'hide_empty' => false,
);
$terms = get_terms( $args );
//
// add current category at beginning
if ($queried_object instanceof WP_Term) {
array_unshift( $terms, $queried_object );
}
//
// Check if any term exists and print them all
if ( ! empty( $terms ) && is_array( $terms ) )
{
echo '<ul>';
foreach ( $terms as $term ) {
printf('<li><a href="%s">%s</a></li>',
esc_url( get_term_link( $term ) ),
$term->name
);
}
echo '</ul>';
}
}
<?php
$args = array(
'taxonomy' => 'your-category-name',
'hierarchical' => true,
'title_li' => '',
'hide_empty' => false
);
?>
<ul>
<?php wp_list_categories( $args ); ?>
</ul>
You can use the wp_list_categories function also for taxonomies.
http://codex.wordpress/Template_Tags/wp_list_categories
I found this, to be more specific i put parent category id for it.
and maybe i will make the result as shortcode.
$term_id = 3;
$taxonomy_name = 'category_name';
$termchildren = get_term_children( $term_id, $taxonomy_name );
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
本文标签: custom taxonomyHow to show category list in WordPress
版权声明:本文标题:custom taxonomy - How to show category list in WordPress 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745529481a2154680.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论