$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Custom taxonomies|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Custom taxonomies

matteradmin8PV0评论

I have created a custom taxonomy 'regionen'.

I have two levels inside the taxonomy

Anden
- Peru
- Bolivian
Conosur
- Chile
- Argentinien

I'm calling the taxonomy on the home page like this ('see below') and it lists all the terms for each post. Example Anden, Peru.

<?php the_terms( get_the_id(), 'regionen' ); ?>

What I would like to do is just call the relevant top level term 'Anden or Conosur' for each post on my index page and the second level term only on my single post pages??

Hopefully that makes sense and someone help explain this to me. Thanks for taking the time to read my problem.

I have created a custom taxonomy 'regionen'.

I have two levels inside the taxonomy

Anden
- Peru
- Bolivian
Conosur
- Chile
- Argentinien

I'm calling the taxonomy on the home page like this ('see below') and it lists all the terms for each post. Example Anden, Peru.

<?php the_terms( get_the_id(), 'regionen' ); ?>

What I would like to do is just call the relevant top level term 'Anden or Conosur' for each post on my index page and the second level term only on my single post pages??

Hopefully that makes sense and someone help explain this to me. Thanks for taking the time to read my problem.

Share Improve this question asked Feb 22, 2019 at 11:27 Rico ShaftRico Shaft 491 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

You can use get_the_terms( int|WP_Post $post, string $taxonomy ) . See Codex detail here.

$my_terms = get_the_terms( get_the_id(), 'regionen' );
if ( ! empty( $my_terms ) ) {
    if ( ! is_wp_error( $my_terms ) ) {
        echo '<ul>';
            foreach( $my_terms as $term ) {
                if($term->parent == 0){
                    echo '<li><a href="' . get_term_link( $term->slug, 'regionen' ) . '">' . esc_html( $term->name ) . '</a></li>'; 
                }
            }
        echo '</ul>';
    }
}
Post a comment

comment list (0)

  1. No comments so far