Support » Fixing WordPress » Limit Number of Term in "get the term list"

  • Hello,

    I use taxonomy terms to create titles and other content and need to limit to the first taxonomy term. Is there a way to do this?

    For example:

    <h2><?php $terms_as_text = get_the_term_list( $post->ID, 'topics') ; echo strip_tags($terms_as_text); ?> Rest of Title</h2>

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this:

    <?php
    $terms = get_the_terms( $post->ID, 'topics' );
    if ( $terms && ! is_wp_error( $terms ) ) :
    $terms = array_values($terms);
    ?>
    <h2><?php echo $terms[0]->name; ?> Rest of Title</h2>
    <?php endif; ?>

    http://codex.wordpress.org/Function_Reference/get_the_terms

    Thanks so much Keesiemeijer! That does work!

    Last question, how do I use term_order so I can display the term by first entered?

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this to display the term with the lowest ID:

    <?php
    $terms = wp_get_object_terms( $post->ID, 'topics', array('orderby' => 'id') );
    if ( $terms && ! is_wp_error( $terms ) ) :
    $terms = array_values($terms);
    ?>
    <h2><?php echo $terms[0]->name; ?> Rest of Title</h2>
    <?php endif; ?>

    wwwaddell

    (@wwwaddell)

    This really helped me so much! Thanks

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Limit Number of Term in "get the term list"’ is closed to new replies.