2017-11-15 5 views
0

リピータフィールドを使用して、コンテンツブロックと画像ブロックをリストとして表示します。しかし、私はすべての画像に1つのクロップサイズを使用したくありません。例えばリピータフィールドのACF複数の画像サイズ

List item 1: Image size 1 
List item 2: Image size 2 
List item 3: Image size 2 
List item 4: Image size 3 

これはさえ可能ですか?

私が現在使用しているマークアップは次のとおりです。

<ul> 

<?php while(have_rows('home-features')): the_row(); 

    // vars 
    $image = get_sub_field('image'); 
    $description = get_sub_field('description'); 
    $url = get_sub_field('url'); 

    ?> 

    <li> 

     <a href="<?php echo $url; ?>"> 
      <div> 
       <h3><?php echo $description; ?></h3> 
      </div> 
     </a> 

    </li> 

<?php endwhile; ?> 

</ul> 
+1

イメージのカウントを保持し、whileループ内でそれをインクリメントするために使用できる変数を作成し、その変数を使用して必要なイメージサイズを決定します。何かが好きです:https://pastebin.com/uBqz0MjE – naththedeveloper

答えて

0

あなたは各中継要素に特定の画像サイズを割り当てることができるようにしたい場合は、その理由だけで、他のACFサブフィールドを追加しません画像サイズの選択肢を備えたリピータ?

新しい「画像サイズ」(image_size)は、サブフィールドの選択]を選択し、このような何かに行くことができます:

thumbnail : Thumbnail 
medium : Medium 
medium_large : Medium Large 
large : Large 
full : Full Size 

をあなたは上記たい場合には、限りの値として、独自のカスタム画像サイズを置き換えることができ各選択項目はカスタム画像サイズ$namesee codex for reference)に対応します。あなたのループで次に

<ul> 

    <?php while(have_rows('home-features')): the_row(); 

    // make sure the ACF return format is set to 'image array' on the image field 
    $image = get_sub_field('image'); 

    // new select field described above 
    $size = get_sub_field('image_size'); 

    // put the two together using the image array values 
    $img_url = $image['sizes'][ $size ]; 

    $description = get_sub_field('description'); 
    $url = get_sub_field('url'); 

    ?> 

    <li> 

    <a href="<?php echo $url; ?>"> 
     <div> 
     <img src="<?php echo $img_url; ?>"> 
     <h3><?php echo $description; ?></h3> 
     </div> 
    </a> 

    </li> 

    <?php endwhile; ?> 

</ul> 

参照

  1. add_image_size
  2. Post Thumbnails
  3. ACF Image Field

注: 私はこのコードをテストしていないので、何か問題がある場合はお知らせください!

関連する問題