0

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.

2 Answers 2

0

&& is the logical and. You need to use . for string concatenation:

$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"]');
Sign up to request clarification or add additional context in comments.

Comments

0

$random_categories[$random_keys[0]] will only ever return the first element from $random_keys. You will need to build your string of IDs before you echo it to your shortcode.

eg,

foreach( $random_keys as $random_key ) {
    $output_ids[] = $random_categories[$random_key];
}

and then implode() the array into a string for your output:

$my_shortcode_string = do_shortcode('[ux_product_categories style="normal" col_spacing="xsmall" columns="5" columns__sm="3" columns__md="5" animate="bounceInUp" ids="' . implode(',', $output_ids) . '"text_size="small" text_padding="0px 0px 0px 0px"]');

(I'm assuming that your shortcode requires the IDs as a comma-separated list, if not change the ',' part of implode(',', $output_ids) to whatever it requires as a separator.)

1 Comment

Guys This is great. Thank you so much. Amit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.