开发者

Wordpress: Limit the number of retults from get_the_term_list

开发者 https://www.devze.com 2023-04-12 01:54 出处:网络
I\'m currently using <?php echo get_the_term_list( $post->ID, \'tag\', \'\', \'\', \'\' ); ?> to return the list of tags attached to a custom post type in index.php and single.php.

I'm currently using <?php echo get_the_term_list( $post->ID, 'tag', '', '', '' ); ?> to return the list of tags attached to a custom post type in index.php and single.php.

Some of the posts have upwards of 20 tags attached, I need to keep these but only display the top 5 on the page.

I've looked at a few options inclu开发者_JAVA技巧ding wp_get_object_terms and get_objects_in_term but can't seem to get the right combination of arguments.

Any ideas?


Looking at the source code for get_the_term_list, there's a filter you can attach to which processes the terms before they're output, so if you put the following in your functions.php:

add_filter( "term_links-tag", 'limit_terms');

function limit_terms($val) {
    return array_splice($val, 0, 5);
}

assuming, of course, that your taxonomy is called 'tag' - if not, change the filter name accordingly.

0

精彩评论

暂无评论...
验证码 换一张
取 消