0

I have 2 taxonomy sets product_cat and tvshows_cat.

There are 12 terms for each of them. A product may get 1 or 12 terms applied.

I use this code to show the term list in the product page:

$cats = get_the_term_list($post->ID, 'product_cat', '', '  ', ''); 
$tvcats = get_the_term_list($post->ID, 'tvshows_cat', '', '  ', ''); 

if (!empty($cats)){
    echo strip_tags($cats, '   ');
}elseif(!empty($tvcats)){
    echo strip_tags($tvcats, '    ');
}

The result is:

Action, Drama, Adventure, Biography, Animation

The problem is it doesn't have enough space to show all of the terms.

I need to limit number of terms to 2 terms.

Question

How can I limit the number of terms applied to two?

1

Instead using get_the_term_list() function, you should use get_the_terms() which will give you directly an array of terms (as get_the_term_list() is using get_the_terms() herself if you look to the source code of the function).

Then that you can build a custom function to get what you want (I will not use implode() function or any other one php function as we want only 2 terms.)

Note: You don't need strip_tags() function here

So your code will be:

// This function goes first
function get_my_terms( $post_id, $taxonomy ){
    $cats = get_the_terms( $post_id, $taxonomy );

    foreach($cats as $cat) $cats_arr[] = $cat->name;

    if ( count($cats_arr) > 1) $cats_str = $cats_arr[0] . ', ' . $cats_arr[1]; // return first 2 terms
    elseif ( count($cats_arr) == 1) $cats_str = $cats_arr[0]; // return one term
    else $cats_str = '';

    return $cats_str;
}

This code goes in function.php file of your active child theme (or theme) or any plugin files…

Then below is your code:

$cats = get_my_terms( $post->ID, 'product_cat' ); 
$tvcats = get_my_terms( $post->ID, 'tvshows_cat' ); 

// Displaying 2 categories terms max
echo $cats . $tvcats;

This code goes on your php templates file

— update — Or without function, your code will be:

// Product categories
$cats = get_the_terms( $post->ID, 'product_cat' );

foreach($cats as $cat) $cats_arr[] = $cat->name;

if ( count($cats_arr) > 1) $cats_str = $cats_arr[0] . ', ' . $cats_arr[1]; // return first 2 terms
elseif ( count($cats_arr) == 1) $cats_str = $cats_arr[0]; // return one term
else $cats_str = '';

// TV shows categories
$tvcats = get_the_terms( $post->ID, 'tvshows_cat' );

foreach($tvcats as $tvcat) $tvcat_arr[] = $tvcat->name;

if ( count($tvcat_arr) > 1) $tvcats_str = $tvcat_arr[0] . ', ' . $tvcat_arr[1]; // return first 2 terms
elseif ( count($tvcat_arr) == 1) $tvcats_str = $tvcat_arr[0]; // return one term
else $tvcats_str = '';

// The Display of 2 categories
echo $cats_str . $tvcats_str;

This code goes on your php templates files

0

We can limit the number of displayed terms with

// Add filter
add_filter( 'get_the_terms', 'wpse_limit_terms' );

// Get terms
$cats   = get_the_term_list($post->ID, 'product_cat', '', '  ', ''); 
$tvcats = get_the_term_list($post->ID, 'tvshows_cat', '', '  ', ''); 

// Remove filter
remove_filter( 'get_the_terms', 'wpse_limit_terms' );

where we define the filter callback as:

function wpse_limit_terms( $terms )
{
    // Adjust to your needs
    $length = 2;

    // Slice the terms array
    if( is_array( $terms ) && count( $terms ) > $length )   
        $terms = array_slice( $terms, $offset = 0, $length );

    return $terms;
}

Update:

It looks like OP only wants to get a comma separated list of term names.

We can alternatively do the following:

$taxonomy = 'product_cat'; // Adjust to your needs
$terms = get_the_terms( get_the_ID(), $taxonomy );
if( is_array( $terms ) && ! empty( $terms ) )
    echo join( ',', wp_list_pluck( array_slice( $terms, 0, 2 ), 'name' ) 

We could also create a general wrapper for this. Here's an untested suggestion:

/**
 * Custom wrapper
 *
 * @return string Comma seperated term attribute values
 */
function get_wpse_terms_attribute_csv( $post_id, $taxonomy, $attribute, $length = 0 )
{
    $csv = '';

    // Nothing to do for non-attributes 
    if( ! in_array( $attribute, [ 'name', 'term_id', 'slug' ] ) )
        return $csv;

    // Nothing to do if taxonomy doesn't exists
    if( ! taxonomy_exists( $taxonomy ) )
        return $csv;

    // Fetch terms
    $terms = get_the_terms( $post_id, $taxonomy );

    // Nothing to do if we don't have a non-empty array of terms 
    if( ! is_array( $terms ) || empty( $terms ) )
        return $csv;

    // Slice the array if needed
    if( (int) $length > 0 )
        $terms = array_slice( $terms, 0, (int) $length )

    // Pluck the terms array   
    $terms = wp_list_pluck( $terms, $attribute );

    // Generate a list of comma separated term attribute values 
    $csv = join( ',', $terms );

    return $csv;   
 ) 

We can then use this within the loop like:

echo get_wpse_terms_attribute_csv( 
    $post_id   = get_the_ID(), 
    $taxonomy  = 'product_cat', 
    $attribute = 'name', 
    $length    = 2 
);

Hopefully you can adjust this further to your needs!

  • is there a way to not use it in function? – Mostafa AHmadi Sep 4 '16 at 10:47
  • Not sure what you mean, can you elaborate? @MostafaAHmadi – birgire Sep 4 '16 at 10:49
  • I use taxonomy list in content-product.php page and just in this page i want to limit number of taxonomy, not all of wordpress – Mostafa AHmadi Sep 4 '16 at 10:50
  • Here we only apply the changes to your two get_the_term_list() functions calls, because we remove the filter callback right afterwards, so it will not affect other terms lists. @MostafaAHmadi – birgire Sep 4 '16 at 10:53
  • I updated the answer with an alternative approach @MostafaAHmadi – birgire Sep 4 '16 at 11:33

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.