WordPress.org

Ready to get started?Download WordPress

Forums

get_the_term_list() - output number of terms associated with current post (7 posts)

  1. mistercyril
    Member
    Posted 3 years ago #

    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.

  2. vtxyzzy
    Member
    Posted 3 years ago #

    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)";
    ?>
  3. mistercyril
    Member
    Posted 3 years ago #

    Hello,

    Thanks for that, unfortunately I get

    unexpected T_VARIABLE

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

    Any ideas?

  4. vtxyzzy
    Member
    Posted 3 years ago #

    Missing semicolon on line above.

  5. mistercyril
    Member
    Posted 3 years ago #

    Now I get

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

  6. vtxyzzy
    Member
    Posted 3 years ago #

    $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)";
                   ?>
  7. mistercyril
    Member
    Posted 3 years ago #

    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)

Topic Closed

This topic has been closed to new replies.

About this Topic