我正在用这样的随机类制作短代码
随机算术生成器:
function getRandomCode(){
$alphanumeric = "0123456789abcdefghijklmnopqrstuvwxyz";
$su = strlen($alphanumeric) - 1;
// echo \'<br/>\';
// // echo rand(0, $su);
// // echo \'<br/>\';
// // echo substr($alphanumeric, rand(0, $su), 3);
// // echo \'<br/>\';
echo substr($alphanumeric, rand(0, $su), 3) .
substr($alphanumeric, rand(0, $su), 2).
substr($alphanumeric, rand(0, $su), 1).
substr($alphanumeric, rand(0, $su), 1);
}
function heading( $atts, $content = null ) {
// [my_heading][/my_heading]
$atts = shortcode_atts( array(), $atts, \'my_heading\' );
// do shortcode actions here
$output = "<h1 class=\\"my-theme-element-". getRandomCode() ." banner-heading test-class-1\\">".$content."</h1>";
return $output;
}
add_shortcode( \'my_heading\',\'heading\' );
随机类为
my-theme-element-".getRandomCode()."
The Problem
:
问题是它没有像预期的那样生成类意味着它是在页面上方生成的,而不是在h1标记内部生成的。我如何解决这个问题?
最合适的回答,由SO网友:user7791702 整理而成
根据@Mark Kaplen的评论:
使您getRandomCode()
返回值,而不是像这样回显
function getRandomCode(){
$alphanumeric = "0123456789abcdefghijklmnopqrstuvwxyz";
$su = strlen($alphanumeric) - 1;
// echo \'<br/>\';
// // echo rand(0, $su);
// // echo \'<br/>\';
// // echo substr($alphanumeric, rand(0, $su), 3);
// // echo \'<br/>\';
return substr($alphanumeric, rand(0, $su), 3) .
substr($alphanumeric, rand(0, $su), 2).
substr($alphanumeric, rand(0, $su), 1).
substr($alphanumeric, rand(0, $su), 1);
}
记住不要更改您的快捷码回调