2014-08-29 4 views
0

고급 맞춤 필드 리피터 플러그인을 사용하여 링크가있는 큰 이미지 격자를 만듭니다. 맨 위에 만드는 최신 항목을 원합니다. 코드를 통해 순서를 어떻게 바꿀 수 있습니까?고급 사용자 정의 필드 리피터 - 역순 정렬 방법 (WordPress)

어떤 도움도 좋은 것입니다. 다음은 출력 버퍼를 사용하여, 내 머리 위로 떨어져 내 현재 코드

<?php if(have_rows('shop_product')):   

      while (have_rows('shop_product')) : the_row(); 

     ?> 

       <div class="shop-item"> 
        <div class="image-hover"> 
         <div class="image-hover-inner"> 

          <img src="<?php the_sub_field('product_image');?>"> 

          <div class="image-caption"> 
          <div class="image-caption-inner"> 
          <a href="<?php the_sub_field('product_url');?>" target="_blank"><?php the_sub_field('product_brand');?><br> 
          Buy Now</a> 
          </div> 
         </div> 
        </div> 
       </div> 
      </div> 

     <?php endwhile; 
     else : 
     endif; 
     ?> 

답변

3

입니다 -하지 가장 깨끗한 방법하지만

<?php $outs = array(); if(have_rows('shop_product')):   

      while (have_rows('shop_product')) : the_row(); ob_start(); 

     ?> 

       <div class="shop-item"> 
        <div class="image-hover"> 
         <div class="image-hover-inner"> 

          <img src="<?php the_sub_field('product_image');?>"> 

          <div class="image-caption"> 
          <div class="image-caption-inner"> 
          <a href="<?php the_sub_field('product_url');?>" target="_blank"><?php the_sub_field('product_brand');?><br> 
          Buy Now</a> 
          </div> 
         </div> 
        </div> 
       </div> 
      </div> 

     <?php $outs[] = ob_get_clean(); endwhile; 
     else : 
     endif; 
     $outs = array_reverse($outs); 
     echo implode($outs); 
     ?> 
2
당신은 배열로 리피터 필드를 얻을 수 get_field('my_repeater')을 사용할 수 있습니다

있는 당신이 할 수있는 루프 오버.

PHP의 array_reverse() 함수를 사용하면 반복 할 수있는 배열이 생깁니다.

유일한 차이점은 ACF의 sub_field 기능을 사용하는 대신 배열 키로 필드에 액세스해야한다는 것입니다. 귀하의 예를 들어

:

<?php $products = array_reverse(get_field('shop_product')); 

foreach ($products as $product): ?> 

    <img src="<?php echo $product['product_image']; ?>"> 
    <a href="<?php echo $product['product_url']; ?>">Buy now</a> 

<?php endforeach; ?> 
관련 문제