【问题标题】:Wordpress acf repeater get first and second images onlyWordpress acf repeater 仅获取第一张和第二张图片
【发布时间】:2016-05-18 21:59:54
【问题描述】:

我在 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]; ?>" />

【问题讨论】:

    标签: php wordpress repeater advanced-custom-fields


    【解决方案1】:

    我刚刚解决了这个问题。对于那些在转发器字段上显示一定数量的图像行时也遇到问题的人。你可以参考我的回答。 下面的代码从转发器字段返回两个图像。如果您希望返回一定数量的图像,只需更改条件 ($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; ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-29
      • 2011-11-08
      • 2011-12-28
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 2019-12-29
      相关资源
      最近更新 更多