2017-05-22 4 views
0

WordPressの詳細カスタムフィールドを使用してイメージギャラリーを作成しました。今度は、新しいdivをエコーし​​、3つの画像ごとに行のクラスで閉じるif文を追加する必要があります。私はそれを自分でしようとしましたが、うまくいきません。どこが間違っていますか?あなたが使用しているため3つのイメージごとに新しい行を追加するPHP

<div class="container"> 
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
     <?php the_content(); ?> 
     <?php endwhile; else: ?> 
     <p>No Content</p> 
    <?php endif; ?> 

    <?php 
     $images = get_field('gallery_images'); 
     $counter = 3; 
    ?> 

    <ul class="gal_list"> 
     <?php foreach($images as $image): ?> 

     <?php 
      if($counter == 3){ 
       echo "<div class="row">"; 
       $counter = 0; 
      } 
     ?> 

      <li class="col-md-4"> 
       <a class="img-responsive" href="<?php echo $image['url']; ?>" rel="<?php the_title() ?>"><img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" /></a> 
      </li> 

      <?php 
       if($counter == 0){ 
        echo "</div>"; 
       } 
       $counter++; 
      ?> 
     <?php endforeach; ?> 
    </ul> 
</div> 
+1

あなたは 'ul'の中で' div'を使うべきではありません。 –

+0

ok私は代わりに何を使うべきですか? – Reece

+0

[モジュロ演算子](http://php.net/manual/en/language.operators.arithmetic.php)を使うと簡単に実現できます。しかし、私はこの段階で何か基本的なものから始めることを強くお勧めします。将来、引用符で引用符を避けるために、良いエディタを選んで作業することをお勧めします。 – Pyromonk

答えて

1

あなたが引用をエスケープする必要が"

変更:

echo "<div class="row">";

へ:

echo "<div class=\"row\">";

+0

私はそれが欲しいと思っていましたが、少なくともコードは壊れていません。なぜ私はスラッシュを追加する必要がありましたか? – Reece

+0

@Reece、引用符は[文字列](http://php.net/manual/en/language.types.string.php)の区切り文字です。 – Pyromonk

0
<div class="container"> 
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
     <?php the_content(); ?> 
     <?php endwhile; else: ?> 
     <p>No Content</p> 
    <?php endif; ?> 

    <?php 
     $images = get_field('gallery_images'); 
     $counter = 3; 
    ?> 

    <?php foreach($images as $image): ?> 

    <!--add a new row after every three images--> 
    <?php 
     if($counter == 3){ 
      echo "<ul class=\"row gal_list\">"; 
     } 
    ?> 

    <li class="col-md-4"> 
     <a class="img-responsive" href="<?php echo $image['url']; ?>" rel="<?php the_title() ?>"><img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" /></a> 
    </li> 

    <!--close the row after three images, also decrement the counter and reset--> 
    <?php 
     $counter--; 
     if($counter == 0){ 
      echo "</ul>"; 
      $counter = 3; 
     } 
    ?> 
    <?php endforeach; ?> 
</div> 

私はこのコードは大丈夫ですか?

関連する問題