2017-11-01 9 views
0

私のhero.phpファイルのdiv image-16-8が表示されていないことに気付きました。wordpress hero foreach()の無効な引数

<ul data-simple-slider> 
      <?php foreach ($images as $image): ?> 
       <li> 
        <div class="image-16-8" style="background-image: url(<?= $image->url; ?>); "></div> 
       </li> 
      <?php endforeach; ?> 
     </ul> 

私はif条件を追加することでsimiliar questionを読んだ:

私は要素を検査するとき、私はここで

でforeachのために供給無効な引数を()取得するコードですforeachの前に問題を修正するようです。

私はPHPの初心者としてこれを行う方法がわかりません。

ご協力いただきありがとうございます。

<?php 

function tbhHeroShortcode($atts) 
{ 

    $values = shortcode_atts(array(
     'images' => '', 
     'first-line' => '', 
     'second-line' => '', 
     'video' => '', 
     'link' => '', 
    ), $atts); 
    ob_start(); 
    ?> 
    <div class="hero"> 
     <? 
     $images = decode_shortcode_data($values['images']); 
     if ($images): ?> 
      <ul data-simple-slider> 
       <?php foreach ($images as $image): ?> 
        <li> 
         <div class="image-16-8" style="background-image: url(<?= $image->url; ?>); "></div> 
        </li> 
       <?php endforeach; ?> 
      </ul> 
     <?php endif; ?> 
     <div class="hero-content"> 
      <div class="hero-content-first-line"> 
       <h1 class="header"><?= decode_shortcode_data($values['first-line']) ?></h1> 

      </div> 
      <h1 class="italic-header"><?= decode_shortcode_data($values['second-line']) ?></h1> 

      <div class="hero-content-cta"> 
       <a class="hollow-button" href="<?= decode_shortcode_data($values['link']) ?>">Learn More</a> 

      </div> 

     </div> 

     <?php if (count($images) > 1): ?> 
      <div class="hero-controls"> 
       <i class="fa fa-chevron-left hero-controls__left" aria-hidden="true"></i> 
       <i class="fa fa-chevron-right hero-controls__right" aria-hidden="true"></i> 
      </div> 
     <?php endif; ?> 
    </div> 
    <?php 
    $component = ob_get_contents(); 
    ob_end_clean(); 
    return $component; 
} 

add_shortcode('tbhHero', 'tbhHeroShortcode'); 
+0

あなたの '$ images'は変数ではなく、ループすることができないことを意味します - あなたが設定している場所を確認してください。 –

+0

ありがとう、更新された質問を参照してください、 '$ images'がどこに設定されているかを示すコードを追加しました。 –

+0

'decode_shortcode_data()'関数を投稿できますか –

答えて

0

もチェックis_iterable http://php.net/manual/en/function.is-iterable.php

PHP5のための素晴らしいポリフィルのマニュアルページを参照してください$imagesis_iterable

if(is_iterable($images)) { 
    // foreach gets indented here 
} 

場合 - ここで

更新は、完全なヒーローdivがありますユーザー(彼らが< 5.4にいないことを望みましょう)

関連する問題