以随机顺序显示某些WordPress类别

时间:2019-06-25 作者:omid chahardoli

我使用此代码显示一些类别:

<?php
   $categories = get_categories( array(
       \'orderby\' => \'name\',
       \'order\'   => \'ASC\',
       \'exclude\' => \'all\',
       \'include\' => \'135,19,124,133,126\',
   ) );
   foreach  ($categories as $category) {
       echo \'<div class="category-list">\';
       echo \'<a href="\' . get_category_link( $category->term_id ) . \' "><div class="image_wrapper2 is-image list-image">\'. do_shortcode(sprintf(\'[wp_custom_image_category term_id="%s"]\',$category->term_id)). \'</div></a>\' ;
       echo \'<div class="image-category-mosje"><h2 class="title-category"><a href=" \' . get_category_link( $category->term_id ) . \' "> \'.$category->name.\' </a></h2></div>\';
       echo \'<span class="category-count"> \' . $category->count . \'</span>\';
       echo \'</div>\';
   }
?>
但按字母顺序显示时出现问题。我希望节目是随机的,每次都有不同的顺序。有人能帮忙吗?

1 个回复
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成

get_categories 不允许您设置randorderby 阿飞,但这没什么大不了的。它返回一个数组,因此在您的情况下,您所要做的就是洗牌该数组:

<?php
   $categories = get_categories( array(
       \'orderby\' => \'name\',
       \'order\'   => \'ASC\',
       \'exclude\' => \'all\',
       \'include\' => \'135,19,124,133,126\',
   ) );
   shuffle ( $categories );
   foreach  ($categories as $category) {
       echo \'<div class="category-list">\';
       echo \'<a href="\' . get_category_link( $category->term_id ) . \' "><div class="image_wrapper2 is-image list-image">\'. do_shortcode(sprintf(\'[wp_custom_image_category term_id="%s"]\',$category->term_id)). \'</div></a>\' ;
       echo \'<div class="image-category-mosje"><h2 class="title-category"><a href=" \' . get_category_link( $category->term_id ) . \' "> \'.$category->name.\' </a></h2></div>\';
       echo \'<span class="category-count"> \' . $category->count . \'</span>\';
       echo \'</div>\';
   }
?>

相关推荐

使用wp_Dropdown_Categories($args)的输出

请温柔点,我对所有这些编码都是新手!我终于找到了如何获得前端下拉字段,允许我从EDD类别中进行选择(我从Codex中获得):<div class=\"gallery-row\"> <li id=\"categories\"> <h2><?php _e( \'Categories:\' ); ?></h2> <form id=\"category-select\" class=\"category-