有可能得到随机项吗?要获取随机帖子,您可以使用WP_Query
和设置\'orderby\' => \'rand\'
.
但有没有办法用条款做到这一点?
我试过这个:
$terms = get_terms( array(
\'taxonomy\' => \'webshops\',
\'hide_empty\' => false,
\'orderby\' => \'rand\',
\'number\' => 6
) );
有可能得到随机项吗?要获取随机帖子,您可以使用WP_Query
和设置\'orderby\' => \'rand\'
.
但有没有办法用条款做到这一点?
我试过这个:
$terms = get_terms( array(
\'taxonomy\' => \'webshops\',
\'hide_empty\' => false,
\'orderby\' => \'rand\',
\'number\' => 6
) );
与普通人不同WP_Query()
, get_terms()
或WP_Term_Query()
没有随机排序。您要么需要自己在SQL中完成,要么获取所有术语和shuffle 他们pulling out 6 要创建随机项数组,请执行以下操作:
// Get all terms
$terms = get_terms( array(
\'taxonomy\' => \'webshops\',
\'hide_empty\' => false,
) );
// Randomize Term Array
shuffle( $terms );
// Grab Indices 0 - 5, 6 in total
$random_terms = array_slice( $terms, 0, 6 );
我正在使用get_terms 函数按字母顺序排列自定义分类法的所有术语。然而,我的客户现在要求将其中一个术语放在最后(因此不按字母顺序排列)。有人对如何实现这一目标的最佳方式有什么建议吗?