Based on a code found here: Assign Wordpress Short code to PHP Variable?
I've created this code to serve list of 5 categories:
add_action( 'woocommerce_after_main_content', 'inject_shortcode', 5 );
function inject_shortcode() {
if( is_shop() ) {
$my_header = '<br><h2 class="head" align="center">TOP BRANDS WE STOCK</h2>';
$my_shortcode_string = do_shortcode('[ux_product_categories style="normal" col_spacing="xsmall" columns="5" columns__sm="3" columns__md="5" animate="bounceInUp" ids="33,20,53,55,59,56,58,57,54" text_size="small" text_padding="0px 0px 0px 0px"]');
echo $my_header;
echo $my_shortcode_string;
}
}
But this only serves with the first 5 categories on the list.
Now.. I want to take it a step forward and randomize the list of categories, So each page refresh, it'll select 5 different categories.
I came up with the following code: But it's not working, What do I misses here?
add_action( 'woocommerce_after_main_content', 'inject_shortcode', 5 );
function inject_shortcode() {
if( is_shop() ) {
$random_categories = array(33,20,53,55,59,56,58,57,54);
$random_keys = array_rand($random_categories,5);
$my_header = '<br><h2 class="head" align="center">TOP BRANDS WE STOCK</h2>';
$my_shortcode_string = do_shortcode('[ux_product_categories style="normal" col_spacing="xsmall" columns="5" columns__sm="3" columns__md="5" animate="bounceInUp"' && 'ids=' && $random_categories[$random_keys[0]] && 'text_size="small" text_padding="0px 0px 0px 0px"]');
echo $my_header;
echo $my_shortcode_string;
}
}
Thank you, Amit.