custom post types - Display 3 level taxonomies

admin2025-01-08  6

I'm a bit confused how to handle this kind of output. I suppose to display three level taxonomies. When a term is clicked i still need to display his level brothers but also his children's... The posts needs to be displayed only when i click the third level taxonomy term.

Please see the mockup below.

I'm a bit confused how to handle this kind of output. I suppose to display three level taxonomies. When a term is clicked i still need to display his level brothers but also his children's... The posts needs to be displayed only when i click the third level taxonomy term.

Please see the mockup below.

Share Improve this question asked Mar 21, 2019 at 9:07 shafshafshafshaf 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Here is general guideline to get what you need. Use get_term() to get 1st, 2nd and 3rd level terms.Documentation here and here.

1st Level: you can display this list of terms directly in php as follows

$taxonomy = "SOME_TAXONOMY";
$terms = get_terms([
     'taxonomy' => $taxonomy,
     'parent' => 0,
     ]);


if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { ?>
<ul>
    <?php 
    foreach ($terms as $term) : ?>
        <li>
            <a href="<?php echo get_term_link($term->term_id); ?>"><?php echo $term->name; ?></a>
        </li>
    <?php endforeach; ?>
</ul>

2nd / 3rd Level Terms
After displaying 1st level terms, you should use ajax action to get child terms of clicked parent term. Pass id of parent term to ajax action function. Documentation for using Ajax with Wordpress is here.

Code for ajax action should look like this.

function wp_ajax_child_terms_action(){

    $taxonomy = "SOME_TAXONOMY";
    $parent = $_GET['id_of_parent_from_ajax'];
    $terms = get_terms([
        'taxonomy' => $taxonomy,
        'parent' => $parent,
     ]);


    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { ?>
       <ul>
           <?php foreach ($terms as $term) : ?>
            <li>
                 <a href="<?php echo get_term_link($term->term_id); ?>"><?php echo $term->name; ?></a>
            </li>
            <?php endforeach; ?>
      </ul>

<?php }  ?>

I hope this may help you start.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736270831a1459.html

最新回复(0)