Wordpress: Category Based Random Shortcode from Array

I am trying to generate some extra money for our single income family this holiday season by building an affiliate site to sell. However, after much research and little help from the WordPress forum:

I am in desperate need of some PHP code to randomly select a do_shortcode from an array based on the conditional category - similar to the Widget Logic plugin but for shortcodes.

I am currenty using:

‘<?php echo do_shortcode(“[asle id=15]”); ?>’

in my single.php page at www.AvengersCollectibles.com/wp to display the shortcode for my “Azon Pro Shopping List” that appears at the bottom of my single post pages but need some code to randomly show 1 of 5 such lists from an array based on each of the 9 categories/characters.

Thus far, something like the following was recommended:

‘<?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]”);

but does not seem to work. Any help would be greatly appreciated and I welcome all comments, concerns, or questions you may have.

PS: I will gladly offer access if necessary and a royalty upon my sale of the site if you are able to successfully solve this problem.

Cory Buford
Virtual Realty Consultant
GateWay Marketing Online

Welcome to SitePoint, GWMO

First I would TEST that shortcode function has been added successfully, and its thus functioning.

add_shortcode(‘shortCodeName’, ‘fooName’);

replacing: shortCodeName for asle and fooName for your function name, of course.

Assuming that’s all in order,

i would make the following change to your code:

<?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;
[B]if ( ! $id ) {
   $key = array_rand( $ids, 1 );
   echo do_shortcode("[asle id=$id]");


(BTW, if the shortcode ‘asle’ already outputs data, you can get rid of the ‘echo’, for efficiency sake)

Hope that helps.

Dear dresden_phoenix,

I thank you for your fast reply and tried pasting your code into the page but keep receiving the following error message:

Method Not Implemented
GET to /wp/wp-admin/theme-editor.php not supported.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

I’m positive that it’s something I did wrong rather than a glitch in your code and would be happy to private message you with access details to the site if interested. I can’t afford much but would be happy to barter sweat equity for your time and assistance if acceptable.