2016-05-02 2 views
1

Imは、現在、私のワードプレスのショートがある:複数のイメージIDをワードプレスショートコードで処理するにはどうすればよいですか?次のようにwordpressのショートコードを介して複数の画像IDを渡そうとし

[lemonslider images="35,46,43,42,41"] 

そして、これは私の機能を以下のように、アイデアは、それがHTMLイメージを返す各画像IDのためでした画像IDの各文字列が、現在それは一つだけを生成します。

// LEMON SLIDER SHORTCODE 

function lemon_slider($atts){ 
$a = shortcode_atts(array(
    'images' => '44', 
    'toshow' => '5', 
), $atts); 

$SlImages = $a['images']; 
$arrlength = count($SlImages); 

for($x = 0; $x < $arrlength; $x++) { 
    $image_attributes = wp_get_attachment_image_src($attachment_id = $SlImages); 
    return ('<img src=' . $image_attributes[0] . ' width=' . $image_attributes[1] . ' height=' . $image_attributes[2] . ' />'); 

} 

} 
add_shortcode('lemonslider', 'lemon_slider'); 

アイブ氏は、foreach文を見られますが、複数の値を返す方法イムはわかりません。

私の出力は1であり、それはあなたのイメージは、カンマで属性を分割する5.

enter image description here

答えて

1

になって、ループから直接返されません。

生成されたイメージタグを文字列に追加し、ループの後の文字列を返します。

function lemon_slider($atts){ 
    $a = shortcode_atts(array(
     'images' => '44', 
     'toshow' => '5', 
    ), $atts); 

    $SlImages = $a['images']; 
    $arrlength = count($SlImages); 

    // split images parameter on the comma 
    $SlImages = explode(",", $a['images']); 

    // define an empty string for the initial return value 
    $ret = ""; 

    foreach ($SlImages as $image) { 
     $image_attributes = wp_get_attachment_image_src($attachment_id = $image); 
     // add each image tag to the return string 
     $ret .= ('<img src=' . $image_attributes[0] . ' width=' . $image_attributes[1] . ' height=' . $image_attributes[2] . ' />'); 
    } 

    // return the result 
    return $ret; 
} 
add_shortcode('lemonslider', 'lemon_slider'); 
+0

同じ結果:/ – MrJoshFisher

+0

あなたの結果は何ですか? – beerwin

+0

Nevermind、忘れてしまったこと、私の答えを更新する。 – beerwin

関連する問題