WordPress:显示随机4个类别,最后3个帖子

时间:2018-04-24 11:18:21

标签: php wordpress random categories

我正忙着建立一个行,该行将随机显示4个类别,其中包含类别标题和3个最新帖子。但我被困在如何以随机顺序获取类别,因为' orderby'不工作......

有人可以帮助我吗?

我正在使用的代码:

 select example_data[0].A, example_data[size(example_data)-1].A
   from
   ( --Your example data
   select array(named_struct("A","1","B","2","C","3"),named_struct("A","4","B","5","C","6"),named_struct("A","7","B","8","C","9")) as example_data
   )s;
OK
1       7
Time taken: 2.72 seconds, Fetched: 1 row(s)

2 个答案:

答案 0 :(得分:1)

代码下方

删除。这只适用于帖子。

$cat_args   = array(
        'orderby' => 'rand',
        'order' => 'ASC'
    );

使用以下代码:

$categories = get_categories();
shuffle ($categories); // Just add shuffle and you will get random categories.

答案 1 :(得分:-1)

你可以用它来获取随机猫:

$categories = get_the_category($my_query->post->ID);
$catCount = count($categories);
//select a random category id
$id = rand(0,$catCount-1);
//cat id
$catId = $categories[$id]->term_id;

and replace to lines like

if(is_array($cat)) {
$categories = get_the_category($my_query->post->ID);
$catCount = count($categories);
//select a random category id
$id = rand(0,$catCount-1);
//cat id
$catId = $categories[$id]->term_id;
} else {
$catId = $cat;
}