簡體   English   中英

從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個或更多,每個都具有它們各自的“示例代碼##”的變體,並且如上所述。 短代碼行允許編輯器指定短代碼並將特定橫幅圖像引入博客文章(使用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文件(用於測試)並在線運行時,它會輸出我列出的隨機橫幅圖像。 萬歲! 成功......有點兒。

看,我現在需要的是編輯器通過短代碼調用隨機結果的方法。 不過,我並不是百分之百確定如何實現這一目標。 原始短代碼基本上是[“代碼塊中使用的id”,“函數名稱”]

我考慮將結果設置為變量然后調用該變量,但我仍然不確定它如何“轉換”(可以這么說)到一個短代碼...

任何人都可以幫助我解決這個難題的最后一部分嗎? 先感謝您!

從array_rand函數的結果創建一個短代碼

我建議你從不同的角度看問題。 而不是從array_rand調用的結果創建一個短代碼,在我看來,你想要定義一個顯示隨機橫幅的短代碼。

如果你想要一個返回隨機橫幅的短代碼,這樣的東西可能會起作用:

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');

我就是這樣做的

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();

砂箱

__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) { ... }
}

基本上,如果已經定義了函數,它就會阻止它定義函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM