从 array_rand 函数的结果创建简码

Creating a shortcode from the results of an array_rand function

我的 WordPress 网站的 functions.php 文件目前有这样的代码块:

function examplecode01() { 
$i = '<a href="/path/" class="exampleclass" id="example-code-01"><img class="example01imgclass" src="path/example01.jpg" alt="Example 01"/></a>';
return $i;
} 
add_shortcode('example-code-01', 'examplecode01');

其中有 5 个或更多,每个都有各自的 "example code ##" 变体,如上所述。简码行允许编辑指定简码并将特定横幅图像拉入博客 post(使用 add_shortcode 的第一个参数),如下所示:

[example-code-01]

我想做的是 随机化 它,这样编辑人员可以在任何地方使用相同的短代码,并且它将是可用的随机横幅图像.

为了解决这个问题,我稍微修改了上面的代码块,如下所示:

function examplecode01() { 
echo '<a href="/path/" class="exampleclass" id="example-code-01"><img class="example01imgclass" src="path/example01.jpg" alt="Example 01"/></a>;
}  

(我删除了短代码行,因为它会导致下一节的输出出现问题——请耐心等待)。

好的,所以有几个修改后的代码块,每个都有自己的图像。在它们的最后,我有一个函数,如下所示:

$functions = array('examplecode01', 'examplecode02', 'examplecode03', 'examplecode04', 'examplecode05'); 
$functions[array_rand($functions)]();

当我将其放入一个空白的 PHP 文件(用于测试)并在线 运行 时,它会从我列出的那些图像中随机输出一个横幅图像。万岁!成功......有点。

看,我现在需要的 是编辑器通过简码调用随机结果的方法。不过,我不是 100% 确定如何做到这一点。原始简码基本上是 ["id used in the code block", "function name"]

我考虑过将结果设置为一个变量然后调用该变量,但我仍然不确定它如何 "convert"(可以这么说)到一个简码...

谁能帮我解决这个难题的最后一部分?提前致谢!

我会这样做

function examplecode01(){
    return __FUNCTION__;
}

function examplecode02(){
    return __FUNCTION__;
}

function examplecode03(){
    return __FUNCTION__;
}

function examplecode04(){
    return __FUNCTION__;
}

function examplecode05(){
    return __FUNCTION__;
}

function examplecode() { 
    $functions = array('examplecode01', 'examplecode02', 'examplecode03', 'examplecode04', 'examplecode05'); 
    shuffle($functions); //randomize array
    $function = array_shift($functions); //get first array element

    return function_exists($function) ? $function() : '';
} 
//add_shortcode('example-code-random', 'examplecode');

echo examplecode();

Sandbox

__FUNCTION__ "magic"常量是当前函数的名称,你可以用你的实际代码替换它,它只是让你更容易看到调用了哪个函数。

老实说,我不会这样做 examplecode01examplecode02(迭代函数),我只会编写一个简短的代码(来统治它们),例如:

[examplecode]  //random
[examplecode banner="1"] //examplecode01

等等,以后容易扩展。像这样:

function examplecode($attr) { 
    $banners = array('example01.jpg', 'example02.jpg', 'example03.jpg', 'example04.jpg', 'example05.jpg'); 
    $atts = shortcode_atts(array(
        'banner' => false
    ), $atts );

    if($atts['banner'] && isset($banners[$atts['banner']-1])){
        //make sure to check if the index exists
        //you could handle this differently, such as not show a banner, show an error message etc...
        $index =  $atts['banner']-1;  //0 based vs 1 based
    }else{
        $index = rand(0, (count($banners)-1));
    }

    //pad with a leading 0 if less then 2
    $i = str_pad($index+1, 2, '0', STR_PAD_LEFT);       

    return '<a href="/path/" class="exampleclass" id="example-code-'.$i.'"><img class="example'.$i.'imgclass" src="path/'.$banners[$index].'" alt="Example '.$i.'"/></a>';
} 
add_shortcode('examplecode', 'examplecode');

看看你是否function{n}并且你想添加一个横幅,你必须制作一个全新的功能和简码。但是,如果您使用短代码属性,则只需将图像添加到数组中。没有 "banner" 属性它是随机的,否则它是数组中的横幅图像编号。由于数组是基于 0 的,因此要使您的横幅保持基于 1 会有些麻烦。您可以通过从 0 开始来简化它。但无论如何...

最后一件事是,每当我编写短代码时,我都会将它们包装在一个存在的函数中,您不必那样做。这只是我喜欢做的事情,只是为了安全。

if(!function_exists('examplecode')){
    function examplecode($attr) { ... }
}

基本上它会阻止它定义已经定义的函数。

Creating a shortcode from the results of an array_rand function

建议你换个角度看问题。在我看来,您不想根据 array_rand 调用的结果创建短代码,而是想定义一个将显示随机横幅的短代码。

如果您想要一个 returns 随机横幅的短代码,这样的方法可能会起作用:

function random_banner() {
    $banner_indexes = ['01', '02', '03', '04', '05'];
    $index = $banner_indexes[array_rand($banner_indexes)];

    return sprintf('<a href="/path/" class="exampleclass" id="example-code-%s"><img class="example%simgclass" src="path/example%s.jpg" alt="Example %s"/></a>', $index, $index, $index, $index);
}

add_shortcode('random-banner', 'random_banner');