I am trying to limit the amount of category tags I output to 5 as well as append a "..." to the end of any items where the output was limited. I've got it mostly working except I can't figure out how to only echo the first 5 tags.
I should mention I only want this to take place on a particular page, but the code I am working with is stored in the functions file but is called by the page in question.
The ideal output would look like this:
Item 1: tag 1, tag 2, tag 3
Item 2: tag 1, tag 2, tag 3, tag 4, tag 5...
This is the code I have so far. I think I need to use array_splice
but am not sure how to implement it.
if ( $type === 'portfolio' ) {
$terms = get_the_term_list( $post->ID, 'royal_portfolio_cats', '', $separator );
$term_array = explode(',',$terms);
$count = sizeof($term_array);
if ($count <= 5) {
echo $terms;
}
elseif ($count > 5) {
$limit_terms = //Do something to output only the first 5 terms with comma separators
echo $limit_terms."...";
}
}
if ( $limit = array_slice($term_array, 0, $max = 5) ) { echo implode( ", ", $limit ); if ( count( $terms ) > $max ) { echo " ..."; } }
after$term_array
$term_array
instead ofif ( count( $terms ) > $max ) { echo " ..."; }
term_links-royal_portfolio_cats
which passes an array to it - but it doesn't pass args so you don't have access tobefore
orafter
in order to add the ellipsis.