WordPress

display 2 random taxonomy terms

<?php
    $max = 2;
    $taxonomy = 'portfolio-category';
    $terms = get_terms($taxonomy, 'orderby=name&order=ASC&hide_empty=1');
 
    // Random order
    shuffle($terms);
 
    // Get first $max items
    $terms = array_slice($terms, 0, $max);
 
    // Sort by name
    usort($terms, function($a, $b){
      return strcasecmp($a->name, $b->name);
    });
 
    // Echo random terms sorted alphabetically
    if ($terms) {
      foreach($terms as $term) {
        echo '<p><a href="' .get_term_link( $term, $taxonomy ) . '" title="' .  sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a></p> ';
      }
    }
 
?>