我有两个函数,我想知道如何随机调用它们。也就是说,PHP代码将从这两个函数中随机选择一个?我该怎么做呢?
示例函数
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>';
}
随机选择两个函数的代码
<?php
$functions = array('one', 'two', 'three'); // remove the open and close parenthesis () in the strings
call_user_func($functions[array_rand($functions)]);
?>
上面的代码不起作用。想知道是否有人能够帮忙?
add_shortcode
接受两个字符串参数,因此在可调用的名称周围添加''
。 - Jite