【发布时间】:2014-09-13 05:18:56
【问题描述】:
所以我有这个函数,我想知道如何随机调用这两个函数。我的意思是,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)]);
?>
上面的代码不起作用。想知道是否有人可以提供帮助?
【问题讨论】:
-
函数名不能带有“-”
-
得到这个错误:函数名必须是第 12 行 D:\Xampp\htdocs\...php 中的字符串
-
add_shortcode接受两个字符串,因此在可调用名称周围添加''。 -
您仍然以错误的方式调用函数。您将它们作为大写字符串,仍然包括括号。阅读我的答案。
-
或者您可能没有使用问题底部的代码?然后请发布错误。 (另外,您正在回显函数的结果,但函数本身在内部回显,从函数调用中删除回显或让函数返回数据而不是回显它)。
标签: php arrays function random