0

hey im trying to figure out how i can display only two terms that are associated with a custom post type. I tried using $wpdb but that doesn't work for me since i need the term links as well. any suggestions?

thanks

2

Maybe this can help. First, get all the post's terms; then, cycle through them with a foreach loop to retrieve and echo each one's link. The last line of code will limit the cycle to the first two terms.

    $terms = wp_get_post_terms(); // If you're not in the loop, you should pass the post's ID as an argument.

    $i = 0;

    foreach ( $terms as $term ) {

        $name = $term->name;
        $href = get_term_link( $term->term_id ); 

        echo '<a href="' . $href . '">' . $name . '</a>';

        if ( ++$i == 2 ) break; // Limit to first 2 tags

    }
1

You can get that using this default function

$args = array(
    'orderby'           => 'name', 
     'order'             => 'ASC',
     'hide_empty'        => true, 
     'exclude'           => array(), 
     'exclude_tree'      => array(), 
     'include'           => array(),
     'number'            => '', 
     'fields'            => 'all', 
     'slug'              => '',
     'parent'            => '',
     'hierarchical'      => true, 
     'child_of'          => 0,
     'childless'         => false,
     'get'               => '', 
     'name__like'        => '',
     'description__like' => '',
     'pad_counts'        => false, 
     'offset'            => '', 
     'search'            => '', 
     'cache_domain'      => 'core'
 ); 
 $terms = get_terms($taxonomies, $args);

More details on this link: https://codex.wordpress.org/es:Function_Reference/get_terms

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.