Metro Detroit WordPress Meetup Message Board › Using do_shortcode() with Random Array in Custom Single.php Template

Using do_shortcode() with Random Array in Custom Single.php Template

Cory B.
CoryBuford
Toledo, OH
Post #: 2
I hope this sort of question is welcome from a newbie and offer my apologies if I am in error.

I am attempting to integrate a conditional to randomly select 1 of 5 shortcodes (Amazon list from “Azon Pro Shopping List” in this case) based on each category at:

http://www.AvengersCollectibles.com/wp­

With that said, I would like the script to randomly select one of 5 shortcodes (5 lists with 5 product categories each). I think I’m on the right track but can not seem to get the code correct:

To clarify, the program I am currently using allows me to build custom lists of Amazon products and adds the list to posts and pages with the shortcode [asle id={list number}](i.e. [asle id=1], [asle id=2], [asle id=3], etc.). Ideally, I wish to rotate them each time the visitor views a new post in that category.

Hope this helps to clarify and, though this is a for profit site, I will be using what I learn for our local Block Watch site, a church site, and a fundraising site for “Feed Your Neighbor” as well and thank you for any and all advice you may be able to offer.

The code I am currently trying to insert into single.php is:

'<?php $ids = array( 43, 15, 8 ); // Default
if (in_category('Captain America'))
$ids = array( 15, 16, 17, 19, 20 );
elseif (in_category('Spiderman'))
$ids = array( 35, 36, 37, 39, 50, 5, 70 );
elseif (in_category('Green Hornet'))
$ids = array( 75, 6, 22, 49 );

$id = 0;
while ( ! $id ) {
$id = array_rand( $ids, 1 );
}
echo do_shortcode("[asle id=$id]");
?>'

PS: Only the shortcodes for the category "Captain America" currently exist but the following did not work either:

'<?php if (in_category('Captain America'))
$ids = array( 15, 16, 17, 19, 20 );

$id = 0;
while ( ! $id ) {
$id = array_rand( $ids, 1 );
}
echo do_shortcode("[asle id=$id]");
?>'
Powered by mvnForum

Our Sponsors

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy