4

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?

5
  • You cannot have a function name with "-" Commented Sep 13, 2014 at 5:31
  • got this error: Function name must be a string in D:\Xampp\htdocs\...php on line 12 Commented Sep 13, 2014 at 5:39
  • add_shortcode takes two strings, so add '' around the callable name. Commented Sep 13, 2014 at 5:44
  • You are still calling the functions the wrong way. You got them as upper case strings, still including the parentheses. Read my answer. Commented Sep 13, 2014 at 5:58
  • Or you might not be using the code in the bottom of your question? Then please post the error. (also, you are echo'ing out the result of the function, but the function itself echos inside, remove the echo from the function call or let the function return the data instead of echo'ing it). Commented Sep 13, 2014 at 6:00

4 Answers 4

5

You can call it something like this:

function one() {
    echo '
        <div id="two-post">
            <a href="' . the_permalink() .'" alt="' . the_title() .'" title="' . the_title() .'">
                ' . the_post_thumbnail('dos') . '

                <div class="entry-meta">
                    <h1>' . the_title() . '</h1>
                    <p>By ' . the_author() . '</p>
                </div>

                <div class="overlay2"></div>
            </a>
        </div>
    ';
}

function two() {
    echo wp_trim_words( get_the_content(), 50, '' );
}

function three() { // function names without "-"
    echo '<div>' . the_author() .'</div>';
}

$functions = array('one', 'two', 'three'); // remove the open and close parenthesis () in the strings

$functions[array_rand($functions)](); // call it!

// or
call_user_func($functions[array_rand($functions)]);
Sign up to request clarification or add additional context in comments.

11 Comments

Thank you ghost! i tried your code unfortunately, i have an error located at this line: echo '<?php echo wp_trim_words( get_the_content(), 50, $more = '...' ); ?>';
you may see my revised function above.
no @KareenLagasca remove the PHP's in that just echo it, check my revision
I removed the <?php so this is now the code: echo wp_trim_words( get_the_content(), 50, $more = '...' ); ?>'; but then, the errors is now in this lines echo '<div><?php the_title(); ?></div>'; and echo '<div><?php the_author(); ?></div>';
@KareenLagasca i revised my answer, take a look
|
3

You can use Switch case here...

function one(){
             //some code;
             }
function two(){
             //some code;
             }
function random_caller(){
          int x = rand(0,1);
          switch(x){
          case 1: one();
          break;
          case 2: two();
          break;
          default: echo "could not run any function";
          break;
            }
            }

Comments

2

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).

<?php
$functions = array("functionOne","functionTwo");
$function = array_rand($functions); // no second param uses default param which is 1, and will only return one entry.
echo $functions[$function]() ."<br>";
?> 

Comments

2

Try this:

function one()   { echo 'ONE'; }
function two()   { echo 'TWO'; }
function three() { echo 'THREE'; }

$functions = array('one', 'two', 'three');
call_user_func($functions[array_rand($functions)]);

Or in a function:

function callRandomFunction($functions)
{
    call_user_func($functions[array_rand($functions)]);
}

Called like:

callRandomFunction($functions);

Comments

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.