Changing WordPress function

  function, php, wordpress

I’m using a WordPress function to enter the word ‘and’ before the last term in calling a term list, like this:

function TermList($taxonomy = 'markup') {
    global $post;
    $term_list = '';
    $terms = get_the_terms($post->ID, $taxonomy);
    $n = 1;
    if ($terms) {
        foreach($terms as $term) {
            if ($n < count($terms)) {
                $term_list .= $term->name . ', ';
            } else {
                $term_list = rtrim($term_list, ', ') .  ' and ' . $term->name;
            }
            ++$n;
        }
    }

    $term_list = rtrim($term_list, ', ');

    return $term_list;
}

I want to change this function so it only displays 5 terms and chooses those 5 terms randomly from all available terms. I don’t know where to start. Who can point me in the right direction?

Source: Ask PHP

LEAVE A COMMENT