WordPress.org

Ready to get started?Download WordPress

Forums

Limit Number of Term in "get the term list" (6 posts)

  1. trostle
    Member
    Posted 1 year ago #

    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!

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    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

  3. trostle
    Member
    Posted 1 year ago #

    Thanks so much Keesiemeijer! That does work!

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

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    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; ?>
  5. wwwaddell
    Member
    Posted 1 year ago #

    This really helped me so much! Thanks

  6. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome :-)

Topic Closed

This topic has been closed to new replies.

About this Topic