• Hello,

    I’ve just started using custom post types and I can’t figure aout how to display the number of termes my cpt is associated with for a given taxonomy.

    For example:
    I have a custom post type named “cars”
    I have two taxonomies named “brands” and “options”
    In options, I have all possible options “sun-roof”, “stick-shift”, “four-wheel drive”…

    I would like to be able to output the number of options a car has (kind of the same way we would display a comment count).

    >> Options : (9)

    The reason why I would like to do this, is that on archive pages rendering the entire list of options for each vehicle kind of messy. So i’d like to be able to give people an indication of how well equipped each vehicle is.

    I am currently using <?php echo get_the_term_list( $post->ID, 'options', '', ', ', '</p>' ) ?>

    Any help would be great,

    Thanks,
    C.

Viewing 6 replies - 1 through 6 (of 6 total)
  • This is untested, but it should work:

    <?php
    $terms = get_the_term_list( $post->ID, 'options', '', ', ', '</p>' )
    $term_array = explode(',',$terms);
    $count = sizeof($term_array);
    echo $terms;
    if ($count) echo ">>Options: ($count)";
    ?>
    Thread Starter mistercyril

    (@mistercyril)

    Hello,

    Thanks for that, unfortunately I get

    unexpected T_VARIABLE

    For this line: $term_array = explode(‘,’,$terms);

    Any ideas?

    Missing semicolon on line above.

    Thread Starter mistercyril

    (@mistercyril)

    Now I get

    Catchable fatal error: Object of class WP_Error could not be converted to string

    $terms = get_the_term_list( $post->ID, 'options', '', ', ', '</p>' );

    The parameter ‘options’ in the line above does not refer to an existing taxonomy.

    I have tested the code below:

    <?php
                   $terms = get_the_term_list( $post->ID, 'category', '', ', ', '</p>' );
                   $term_array = explode(',',$terms);
                   $count = sizeof($term_array);
                   echo "<p>TERMS: $terms</p>";
                   if ($count) echo ">>Options: ($count)";
                   ?>
    Thread Starter mistercyril

    (@mistercyril)

    Oh wow, thank you. I don’t know what I did wrong the first time around but it works perfectly. So for those interested in using this, here is the clean version :

    The following code outputs the number of terms associated to current post for the specified taxonomy.

    The ‘category’ (line 2) should be replaced with your taxonomy. (i.e. ‘your-taxonomy’).
    The ‘Options:’ (line 5) should be replaced with whatever label you want to use. (i.e. ‘Your label:’)

    <?php
        $terms = get_the_term_list( $post->ID, 'category', '', ', ', '</p>' );
        $term_array = explode(',',$terms);
        $count = sizeof($term_array);
        if ($count) echo "Options: ($count)";
    ?>

    This example outputs => Options:(X)

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘get_the_term_list() – output number of terms associated with current post’ is closed to new replies.