Wordpress acf中继器仅获取第一和第二图像

时间:2016-02-09 03:04:17

标签: php wordpress repeater advanced-custom-fields

我在wordpress网站上使用acf插件。我正在使用带有图片子字段的转发器字段,该字段显示在博客帖子上。我的目标是只显示要显示在主页上的博客文章中的第一张和第二张图片。

我尝试在acf站点文档中使用此代码但无效但代码无效。有人能知道这个问题吗?

         <?php while ($the_query -> have_posts()) : $the_query ->
            the_post(); 
            $postqID = get_the_ID();
          ?>

          <?php

          $rows = get_field('post_images', $postqID ); // get all the rows
          $first_row = $rows[0]; // get the first row
          $first_row_image = $first_row['post_image' ]; // get the sub field value 

          // Note
          // $first_row_image = 123 (image ID)

          $image = wp_get_attachment_image_src( $first_row_image, 'full' );
          // url = $image[0];
          // width = $image[1];
          // height = $image[2];
          ?>
          <img src="<?php echo $image[0]; ?>" />

1 个答案:

答案 0 :(得分:0)

我刚刚解决了这个问题。对于那些在转发器字段上显示特定行数的图像也存在问题的人。你可以参考我的答案。 下面的代码返回转发器字段中的两个图像。如果你想返回一定数量的图像,只需更改条件($ i> 1)。

          <?php

          $i = 0;
          if(get_field('post_images', $postqID)):
          ?>
            <?php while (has_sub_field('post_images',$postqID)): ?>

              <img src="<?php the_sub_field('post_image')['url']; ?>"></img>

            <?php

              $i++;
              if($i > 1)
              {
                break;
              }
            ?>
            <?php  endwhile; ?>
          <?php endif; ?>