从数组中包含的类别中获取随机类别名称,WordPress


Get random category name from categories that are included in array, WordPress

我正在尝试获取一个随机类别名称。此随机类别必须来自数组中的多个类别。我已经尝试了下面的代码,但是当我这样做时,我只会获得包含的数组 (5) 中第一个类别的类别名称(和 id)。

我如何让其他人也一起玩?

<?php 
$args = array('hide_empty' => 1, 'include' => 5,14,15,19,20,25,27,28,29,31,33,141);
$categories = get_categories ($args);
  if(!empty($categories)) :
  $random_category = $categories[rand(0, (count($categories)-1))];
    echo $random_category->slug . ", " . $random_category->term_id; 
  endif; 
?>

输出不应该是一个数组,而是一个字符串。以下代码将起作用。

<?php 
$categories = get_categories('include=5,14,15,19,20,25,27,28,29,31,33,141&hide_empty=1');
if(!empty($categories)) {
    $random_category = $categories[rand(0, (count($categories)-1))];
    echo $random_category->slug . ", " . $random_category->term_id; 
} 
?>