• Resolved Niura

    (@niura)


    Hello!

    I’ve a series of Custom Post Types.
    Those posts have some categories and i need to display those value within the loop.

    So i retrive my custom post type:

    $args = array( 'post_type' => 'store', 'store_type' => 'MyStore', 'posts_per_page' => '-1');
    $myQuery = new WP_Query($args);
    if ($myQuery->have_posts()) :
    while ($myQuery->have_posts()) : $myQuery->the_post();
    the_title();
    //here i want to display terms of the current post
    $terms = get_the_terms( $post->ID , 'store_brand' );
    if ( is_array( $terms )) {
    foreach ($terms as $term) {
    	echo $term->name . '<br/>';
    }
    }
    endwhile;
    wp_reset_postdata();
    endif;

    The problem is that when i have to display term name, for some reason some posts have categories but aren’t displayed and other have categories and are displayed many times (the same).

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Are we talking the category taxonomy that comes with WordPress out of box? Or are you referring to your store_brand taxonomy above as Category?

    Curious because it sounds like it could be multiple taxonomies coming into play here, which I could see adding to the confusion.

    Also in regards to the use of WP_Query, by chance are you trying to create your own archive loop for the post type? Or just display in a certain part of a given page/page type and not be what I’ll call a normal archive.

    Thread Starter Niura

    (@niura)

    Hello!

    yesterday afternoon i solved by myself… within the loop i use this code to get all the terms of my cpt.

    $term_list = wp_get_post_terms($post->ID, 'store_brand', array("fields" => "all"));
    $elencocategorie='';
    foreach($term_list as $term_single) {
    	$elencocategorie.= $term_single->name . " - ";
    }
    $elencocategorie = rtrim($elencocategorie, "- ");
    echo "$elencocategorie</p>";

    But thanks for your support!

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Awesome to hear you figured it out in the end. Let us know if you need anything else.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Can’t display CPT terms’ is closed to new replies.