最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Output assigned custom taxonomy to custom post type

programmeradmin1浏览0评论

I created a custom post type called "capitulo" and custom taxonomy called "serie" and i wanna output asigned taxonomy to custom post with link.

Here is my code

                            <?php //Do something if a specific array value exists within a post

                        $term_list = wp_get_post_terms($post->ID, 'serie', array("fields" => "all"));

                        $terms = get_terms( 'serie' );

                        foreach ( $terms as $term ) {

                            // The $term is an object, so we don't need to specify the $taxonomy.
                            $term_link = get_term_link( $term );

                            // If there was an error, continue to the next term.
                            if ( is_wp_error( $term_link ) ) {
                                continue;
                            }

                            // Then you can run a foreach loop to show the taxonomy terms infront.
                            foreach ( $term_list as $term_single ) {

                                echo '<h2><a href="' . esc_url( $term_link ) . '">' . $term_single->name . '</a></h2>';

                            } 
                        }

                        ?>

it output a link to all taxonomy terms with link for all custom post. Look

I created a custom post type called "capitulo" and custom taxonomy called "serie" and i wanna output asigned taxonomy to custom post with link.

Here is my code

                            <?php //Do something if a specific array value exists within a post

                        $term_list = wp_get_post_terms($post->ID, 'serie', array("fields" => "all"));

                        $terms = get_terms( 'serie' );

                        foreach ( $terms as $term ) {

                            // The $term is an object, so we don't need to specify the $taxonomy.
                            $term_link = get_term_link( $term );

                            // If there was an error, continue to the next term.
                            if ( is_wp_error( $term_link ) ) {
                                continue;
                            }

                            // Then you can run a foreach loop to show the taxonomy terms infront.
                            foreach ( $term_list as $term_single ) {

                                echo '<h2><a href="' . esc_url( $term_link ) . '">' . $term_single->name . '</a></h2>';

                            } 
                        }

                        ?>

it output a link to all taxonomy terms with link for all custom post. Look

Share Improve this question edited May 15, 2019 at 11:35 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked May 15, 2019 at 10:54 Marcelo HerreraMarcelo Herrera 32 bronze badges 1
  • 2 It's about something like that: echo get_the_term_list( $post->ID, 'serie', '<h2>', '</h2><h2>', '</h2>' ); ? You can find more about get_the_term_list() in documentation. – nmr Commented May 15, 2019 at 11:18
Add a comment  | 

1 Answer 1

Reset to default 0

Here’s the line that causes your problem:

$terms = get_terms( 'serie' );

You use get_terms function which retrieves the terms in a given taxonomy.

And you want to get terms assigned to given post, so you should use get_the_terms which retrieves the terms of the taxonomy that are attached to the post.

So change the line above to:

$terms = get_the_terms( get_the_ID(), 'serie' );
发布评论

评论列表(0)

  1. 暂无评论