2017-05-12 21 views
0

一意のIDを持つビデオプレーヤーコンテナdivを動的に生成したい。要素は、ワードプレスでビジュアルコンポーザを介して生成され、最終 HTML は、このようななものになります。ユーザーは自分が好きなようユニークなIDを持つなど、多くのdiv要素を追加することができるはず動的に生成されたdivの増分ID

<div style="width: 100%; display: inline-block; position: relative;"> 
<div style="margin-top: '. $custom_margin .'"></div> 
<div id="player_1" style="position:absolute;top:0;left:0;right:0;bottom:0"></div> 
    </div> 
<div style="width: 100%; display: inline-block; position: relative;"> 
<div style="margin-top: '. $custom_margin .'"></div> 
<div id="player_2" style="position:absolute;top:0;left:0;right:0;bottom:0"></div> 
    </div> 
<div style="width: 100%; display: inline-block; position: relative;"> 
<div style="margin-top: '. $custom_margin .'"></div> 
<div id="player_3" style="position:absolute;top:0;left:0;right:0;bottom:0"></div> 
</div> 

を。これは私のPHPが今どのように見えるかです: EDIT:関数は、すべて単一のビデオコンテナ

public function vc_custvideo_html($atts){ 

     extract(
      shortcode_atts(
       array(
        'custom_width' => '', 
        'custom_height' => '', 
        ), 
        $atts 
       ) 
      ); 
     $percent = $custom_height/$custom_width; 
     $custom_margin = number_format($percent * 100, 2) . '%'; 
     $html = ''; 
     $html.= '<div style="width: 100%; display: inline-block; position: relative;">'; 
     $html.= '<div style="margin-top: '. $custom_margin .'"></div>'; 
     $html.= '<div id="player_" style="position:absolute;top:0;left:0;right:0;bottom:0"></div>'; 
     $html.= '</div>'; 
     return $html; 
    } 

のために呼ばれて、私はユニークなIDを生成するためにforeachループを使用する必要がありますが、私は本当に新しいんだということを理解することができますPHPで私は助けが必要です。

+0

'$ atts'は**シングル**ビデオプレイヤーのデータですか? –

+0

は、それぞれのビデオごとに、またはそれらをすべて生成するための関数 'vc_custvideo_html'です。これにより、違います – Kaddath

答えて

0

機能を一度ビデオあたりと呼ばれていると仮定すると

$i = 0; 
foreach ($playerInstance as $k => $currPlayer { 
    vc_custvideo_html($currPlayer, $i) 
    $i++; 
} 
public function vc_custvideo_html($atts, $index){ 

    extract(
     shortcode_atts(
      array(
       'custom_width' => '', 
       'custom_height' => '', 
       ), 
       $atts 
      ) 
     ); 
    $percent = $custom_height/$custom_width; 
    $custom_margin = number_format($percent * 100, 2) . '%'; 
    $html = ''; 
    $html.= '<div style="width: 100%; display: inline-block; position: relative;">'; 
    $html.= '<div style="margin-top: '. $custom_margin .'"></div>'; 
    $html.= '<div id="player_'.$index.'" style="position:absolute;top:0;left:0;right:0;bottom:0"></div>'; 
    $html.= '</div>'; 
    return $html; 
} 

またはループのために多分

0

と何かのように、外側のforeachとカウンタを使用します。

$counter=1; 
foreach($arrayOfVideos as $video){ 
    $this->vc_custvideo_html($video, $counter); 
    $counter++; 
} 

内部あなたの関数は、$counter変数を使用してください:

// [...] 
$html.= '<div style="margin-top: '. $custom_margin .'"></div>'; 
$html.= '<div id="player_"' . $counter . ' style="position:absolute;top:0;left:0;right:0;bottom:0"></div>'; 
$html.= '</div>'; 
+0

残念ながら、WordPressは私に構文エラー、予期しない '$ counter'(T_VARIABLE)、関数(T_FUNCTION)を期待していますが、関数の外でforeachを使用しています: – SharPei

+0

ビデオ情報の配列を渡す関数の中にカウンタを含めてforeachをラップします。 –

関連する問題