Php - rotation of shortcodes

I have what I think is a simple question but for some reason using any type of ararys won’t work, at least as I tried.

I have two or three short codes I want to rotate using php but I’m not sure how to get only one to show at a time and what options I have for rotating.

Here are the lines of code

<?php echo do_shortcode(‘[content 1]’); ?>
<?php echo do_shortcode(‘[content 2]’); ?>

I just want one of them at a time to show up on the page. Do you know how I can do that?

Thank you for your help,

Jeff

Never worked with shortcode before but for standard arrays you could do something like this. You probably could adapt to shortcode.

<?php
$do_shortcode = array(
"content1" => "Hello there.",
"content2" => "Hi there.",
"content3" => "Ho there."
);
$cnt = count($do_shortcode);
$n = mt_rand(1,$cnt);
$key = "content$n";
?>

<html>
<body>
<?php echo "{$do_shortcode[$key]}"; ?>
</body>
</html>

Drummin,

Thanks for the code. That is what I have used in the past but with shortcodes I cannot get it to work. The php code I showed in the original post works great, but is will show both at the same time. I need only one to show and for them to be rotated.

I have been going nuts with this, it cannot be that difficult, I must be overlooking something.

Thanks again for the help, does anyone have any other suggestions?

Can’t you generate the number and add it to the key? Assuming they are numbered “content 1”, “content 2” etc.

<?php
$cnt = 2; //Number of shortcodes
$n = mt_rand(1,$cnt);
?>

<html>
<body>
<?php echo do_shortcode("[content $n]"); ?>
</body>
</html

Drummin,

Can you show me how you would use

<?php echo do_shortcode('[shortcode content 1]'); ?>
<?php echo do_shortcode('[shortcode content 2]'); ?>

With your second suggestion. I think you are thinking on the lines of what I thought but I cannot get the two shortcodes to work in the code.

Could you please write it out the way it would be on the page.

Thank You,

Jeff

Can you replace the two lines you have with this?

<?php
$cnt = 2; //Number of shortcodes
$n = mt_rand(1,$cnt);
echo do_shortcode("[content $n]");
?>

The problem is the shortcode works like this [this shortcode], it will be converted to whatever the shortcode represents.

example: [shortcode 1] will display a form
[shortcode 2] will display a image
[shortcode 3] will display link

whatever is in the must be a shortcode with a function already written. So if I use my example line from the other post: <?php echo do_shortcode(‘[shortcode content 1]’); ?> [shortcode content 1] would display. The have to also show on the page so the script knows it’s a shortcode.

I sure hope all that makes sense.

Thanks,

Jeff

It doesn’t make sense, but that’s okay.

if $n is 1, wouldn’t the line echo
do_shortcode(“[content 1]”);

Maybe it’s the full quotes “[content 1]”? I don’t know.

You could also wrap these lines in IF statements like

<?php
$cnt = 2; //Number of shortcodes
$n = mt_rand(1,$cnt);
	  
if(isset($n) && $n==1){
	echo do_shortcode('[content 1]');
}elseif(isset($n) && $n==2){
	echo do_shortcode('[content 2]'); 
}else{
	echo do_shortcode('[content 1]');
}
?>

Drummin,

That was it, the quotes. It worked, thank you for the help! This was driving me nuts.

Thanks again,
Jeff