Wordpress - 从循环内的转发器字段中仅拉出4个图像

时间:2012-01-15 17:58:52

标签: php wordpress

我想知道是否有人可以帮助我做一些可能很简单的事情。

我正在创建一个基于游戏的网站,我只是在目录页面上工作,其中将显示所有游戏的列表。我正在使用名为“Advanced Custom Fields”的插件为每个游戏创建所有自定义字段。

目前我正在进行循环以吸引每场比赛: -

<?php $loop = new WP_Query(array('post_type' => 'games', 'posts_per_page' => 10)); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

然后在此循环中调用游戏的不同自定义字段(标题,屏幕截图,说明,下载按钮等...)。但是我遇到了一个问题,截至屏幕截图,我可以很高兴地抓住所有截图,但我只想拔出4.我已经搜索了所有线程,查看了文档,点击谷歌,但无论方法我已经尝试过我还没有取得任何成功:(

我在上面循环中删除屏幕截图的当前代码是: -

<?php if(get_field('screenshots')): ?>
<ul class="screenshots">
<?php while (the_repeater_field('screenshots')): ?>

    <li><a href="<?php the_sub_field('large_screenshot'); ?>" data-fancybox-group="button" class="fancybox"><img src="<?php the_sub_field('thumbnail_screenshot'); ?>" title="<?php the_sub_field('screenshot_title'); ?>"/></a></li>

<?php endwhile; ?>
</ul> <!--- end of screenshots !-->
<?php endif; ?>

有没有办法将我的结果限制为总共4而不是显示所有可用的屏幕截图?随机4将是一个更好的选择,但不是最重要的。

我确信这个解决方案远比我迄今为止尝试过的一些没有成功的事情简单得多。

非常感谢您收到的任何帮助。

干杯

罗布

4 个答案:

答案 0 :(得分:1)

我对您使用的插件了解不多。但一个简单的解决方案是使用这样的计数器:

<?php $i=0; ?>
<?php while (the_repeater_field('screenshots')): ?>

<li><a href="<?php the_sub_field('large_screenshot'); ?>" data-fancybox-group="button" class="fancybox"><img src="<?php the_sub_field('thumbnail_screenshot'); ?>" title="<?php the_sub_field('screenshot_title'); ?>"/></a></li>

<?php if($i<4) $i++;
      else break; ?>
<?php endwhile; ?>

答案 1 :(得分:0)

我很好奇你如何运行超过1个循环,根据定义它应该像你的经验一样工作但是由于某种原因,当我发布相同的概念时,我只能得到一段时间来产生一个结果,即使有10张图片。

while(the_repeater_field('horse_images')): 
    $image_id = get_sub_field('image');
    $image_src = wp_get_attachment_image_src($image_id,medium);
    echo '<img src="'.$image_src[0].'">';
endwhile;

我想知道如果你用&amp;&amp;&amp; $ i&lt; = 4

<?php if(get_field('screenshots')): ?>
<?php $i = 0; ?>
<ul class="screenshots">
   <?php while (the_repeater_field('screenshots') && $i <= 4 ): ?>

   <li><a href="<?php the_sub_field('large_screenshot'); ?>" data-fancybox-group="button" class="fancybox"><img src="<?php the_sub_field('thumbnail_screenshot'); ?>" title="<?php the_sub_field('screenshot_title'); ?>"/></a></li>

   $i++
<?php endwhile; ?>

答案 2 :(得分:0)

查找

<?php if( have_rows('gallery_img') ): ?>
  <div class="gl-images">
    <div class="flexslider">
      <ul class="slides">
      <?php $i=0; ?>
      <?php while( have_rows('gallery_img') ): the_row(); 
        // vars
        $image = get_sub_field('img_item');
      ?>
      <?php if( $image): ?>
        <li class="slide">
          <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
        </li>
      <?php endif; ?>
      <?php if($i>1) $i++;
        else break; 
      ?>
     <?php endwhile; ?>
     </ul>
  </div>
  </div>
<?php endif; ?>

答案 3 :(得分:0)

<?php if (have_rows('service_priceing')): ?>
<?php $n=0; while (have_rows('service_priceing')): the_row(); ?>
<?php $package = get_sub_field('package'); ?>
<?php $rent = get_sub_field('rent'); ?>
<?php $price = get_sub_field('price'); ?>
<div class="columns price-columns">
   <ul class="price">
      <li class="header" style="background-color:<?php if($n==1){ echo "Red"; }else{echo "Blue";}?>"><?php echo $package; ?></li>
      <li class="dates">
         <p><?php echo $rent; ?></p>
      </li>



   </ul>
</div>
<?php $n++; if($n == 3){ break; } endwhile; ?>
<?php endif; ?>