So I have this functions, I was wondering how can I call the two function randomly. I mean, the php code will randomly select from the two? how can I do that?
Example Functions
function one() {
echo '
<div id="two-post">
<a href="<?php the_permalink(); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail('dos'); ?>
<div class="entry-meta">
<h1><?php the_title(); ?></h1>
<p>By <?php the_author(); ?></p>
</div>
<div class="overlay2"></div>
</a>
</div>
';
}
function two() {
echo '<div class="two">' . wp_trim_words( get_the_content(), 50, '' ) . '</div>';
}
function three() { // function names without "-"
echo '<div class="third">' . the_author() .'</div>';
}
The code for selecting the two functions randomly
<?php
$functions = array('one', 'two', 'three'); // remove the open and close parenthesis () in the strings
call_user_func($functions[array_rand($functions)]);
?>
The code above doesn't work. Was wonder if someone could help?
You can call it something like this:
Remove the parentheses in the array with function names, like:
$a=array("FUNCTION-ONE","FUNCTION-TWO");
And add them on call:echo $a[$random_keys[0]]() . "<br>";
Also, PHP will be a bit annoyed with function names containing the
-
character (cant name functions like that), so try rename the functions to something like:functionOne
(which would also more fit the php standard).You can use Switch case here...
Try this:
Or in a function:
Called like: