0

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.

2
  • This should help: How to list custom taxonomy categories? – nmr May 2 '19 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 May 2 '19 at 11:03
0

To 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>';
    }
}
0
<?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.org/Template_Tags/wp_list_categories

0

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>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.