【问题标题】:Advanced Custom Fields Repeater - How to reverse order (WordPress)高级自定义字段中继器 - 如何反转顺序 (WordPress)
【发布时间】:2014-08-29 10:29:00
【问题描述】:

我正在使用高级自定义字段中继器插件来创建一个带有链接的大图像网格。我想要我在顶部创建的最新项目,所以我要问的是如何通过代码反转顺序?

任何帮助都会很棒。这是我当前的代码

<?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;
        ?>

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    您可以使用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; ?>
    

    【讨论】:

      【解决方案2】:

      它可能无法回答实际问题,但可能会解决问题。 如果要显示中继器的行,则可以将 CSS display:flex 与 flex-flow:row/column-reverse 一起使用;

      因为可能只需要交换订单来显示它

      例子:

      display:flex;
      flex-flow:row-reverse;
      

      【讨论】:

        【解决方案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);
                ?>
        

        【讨论】:

          猜你喜欢
          • 2018-05-19
          • 2018-08-15
          • 1970-01-01
          • 1970-01-01
          • 2016-06-10
          • 2017-08-28
          • 2012-08-10
          • 2015-01-21
          • 2017-05-01
          相关资源
          最近更新 更多