Display the terms of a custom post Taxonomy

Asked

Viewed 692 times

1

am with the following problem in creating a wordpress theme.

I created CPT called child and for it I created a tag taxonomy. Everything is working normally but when I call this CPT post in single-child.php I can’t list the words of the tags associated with this post, I always get an empty array as a response.

Below is the CPT code:

$labels = array(
    'name'               => 'Criança',
    'singular_name'      => 'Criança',
    'menu_name'          => 'Criança',
    'name_admin_bar'     => 'Criança',
    'add_new'            => 'Adicionar Novo',
    'add_new_item'       => 'Adicionar Novo Item',
    'new_item'           => 'Novo Item',
    'edit_item'          => 'Editar Item',
    'view_item'          => 'Visualizar Item',
    'all_items'          => 'Todos os Item',
    'search_items'       => 'Pesquisar',
    //'parent_item_colon'  => 'Parent Books:',
    'not_found'          => 'Nenhum Item Encontrado',
    'not_found_in_trash' => 'Nenhum Item na Lixeira'
);

$args = array(
    'labels'             => $labels,
    'description'        => 'Posts de Atenção a Criança',
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'rewrite'            => array( 'slug' => 'atencao-a-crianca' ),
    'capability_type'    => 'post',
    'has_archive'        => true,
    'hierarchical'       => false,
    'menu_position'      => 8,
    'menu_icon'          => 'dashicons-groups',
    'taxonomies'         => array( 'tags' ),
    'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt' )
);

register_post_type( 'crianca', $args );

Taxonomy:

$labels = array(
    'name'                       => _x( 'Tags', 'taxonomy general name', 'textdomain' ),
    'singular_name'              => _x( 'Tag', 'taxonomy singular name', 'textdomain' ),
    'search_items'               => __( 'Procurar Tags', 'textdomain' ),
    'popular_items'              => __( 'Popular tags', 'textdomain' ),
    'all_items'                  => __( 'Todas as Tags', 'textdomain' ),
    'parent_item'                => null,
    'parent_item_colon'          => null,
    'edit_item'                  => __( 'Editar Tag', 'textdomain' ),
    'update_item'                => __( 'Atualizar Tag', 'textdomain' ),
    'add_new_item'               => __( 'Adicionar Nova Tag', 'textdomain' ),
    'new_item_name'              => __( 'Nome da Nova Tag', 'textdomain' ),
    'separate_items_with_commas' => __( 'Separe tags com virgulas', 'textdomain' ),
    'add_or_remove_items'        => __( 'Adicionar ou Remover Tags', 'textdomain' ),
    'choose_from_most_used'      => __( 'Escolha entre as tags mais usadas', 'textdomain' ),
    'not_found'                  => __( 'Nenhuma Tag Encontrada.', 'textdomain' ),
    'menu_name'                  => __( 'Tags', 'textdomain' ),
);

$args = array(
    'hierarchical'          => false,
    'labels'                => $labels,
    'show_ui'               => true,
    'show_admin_column'     => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var'             => true,
    'rewrite'               => array( 'slug' => 'tags' ),
);

register_taxonomy( 'tags', 'crianca', $args );

single-child.php

<div class="artigo">
                    <?php 

                        if(have_posts()) {

                            while(have_posts()) {
                                the_post();
                                if(has_post_thumbnail()) {
                                    ?>
                                    <img class="img-responsive" src="<?php the_post_thumbnail_url(); ?>">
                                    <?php   
                                }
                                ?>                                  
                                <h1 class="recemNascidoConteudoH1"><?= the_title(); ?></h1>
                                <div class="dataArtigo">
                                    <span><i class="icon ion-android-calendar"></i><?= get_the_date("j M Y" ); ?></span>
                                </div>

                                <?php

                                the_content( );
                                ?>                        
                </div>
                <div class="row tagsArtigo">
                    <div class="col-md-12">
                        <span class="tagsArtigo">
                        <i class="icon ion-pricetag"></i>
                        <?php 

                        $tags = get_terms( 'tags');   
                        var_dump(wp_get_object_terms( $post->ID , 'tags' ));
                        $i = 0;        
                        foreach($tags as $tag) {
                            $term_list[$i] = '<a href="' . esc_url( get_term_link( $tag ) ) . '">' . $tag->name . '</a>';
                            $i++;
                        }     
                        echo "Tags: " . implode(', ', $term_list);  
                        ?>
                        </span>
                    </div>
                </div>
                                <?php
                            } //fim do while
                        } //fim do if  

                    ?>
                <!-- fim do POST -->

get_terms() only gives me all the existing tags but I only need the ones associated with the post.

Already wp_get_object_terms() returns me an empty array.

Someone can adjust me ?

  • https://codex.wordpress.org/Function_Reference/wp_get_post_terms

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.