从PHP和Timber / Twig中的“高级自定义字段”对转发器字段进行排序 [英] Sorting a repeater field from Advanced Custom Fields in PHP and Timber/Twig

查看:184
本文介绍了从PHP和Timber / Twig中的“高级自定义字段”对转发器字段进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Twig 1.34的WordPress Timber插件实现对WordPress插件高级自定义字段(ACF)中转发器字段的输出进行排序。从下面的ACF中排序的基本PHP示例来自 https: //www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/ ,在ACF论坛中没有我的问题的可用答案。

I'm trying to sort the output of a repeater field from the WordPress plugin Advanced Custom Fields (ACF) using the WordPress Timber plugin implementation of Twig 1.34. The basic PHP example to sort from ACF below is from https://www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/ and there are no usable answers for my question in the ACF forums.

所以我正在尝试将此函数从示例转换为Timber / Twig:

So I'm trying to convert this function from the example to Timber/Twig:

// get repeater field data
$repeater = get_field('repeater');

// vars
$order = array();

// populate order
foreach( $repeater as $i => $row ) {
    $order[ $i ] = $row['id'];
}

// multisort
array_multisort( $order, SORT_DESC, $repeater );

// loop through repeater
if( $repeater ): ?>

    <ul>

    <?php foreach( $repeater as $i => $row ): ?>

        <li><?php echo $row['id']; ?>. <?php echo $row['name']; ?></li>

    <?php endforeach; ?>

    </ul>

<?php endif; ?>

我在 view.twig 起作用的文件如下。我的ACF转发器字段称为时间轴,有两个子字段,日期和描述。

What I have implemented in a view.twig file that works is below. My ACF repeater field is called timeline, and has two subfields, date and description.

{% set repeater = timeline %}

   {% for i, row in repeater %}

      {{ row.date}}

      {{ row.description}}

  {% endfor %}

这将输出该字段的现有字段(数组) ACF转发器时间轴-日期和描述这两个字段-以最早的日期顺序排列,这是默认的,例如:

That outputs the existing fields (an array) of the ACF repeater timeline - the two fields date and description - in the date order of oldest first, which is the default, like this:


2010年1月

Jan 2010

Lorum Ipsum等等等等

Lorum Ipsum blah blah blah

Jul 2011

Lorum Ipsum等等等等Lorum Ipsum

Lorum Ipsum blah blah blah Lorum Ipsum

但是我想按最新日期排序。

根据此答案,我需要创建一个使用 Twig_SimpleFilter 的过滤器函数,并对转发器字段数组进行排序。

According to this answer Twig_Error_Syntax for "Unknown filter" with a Twig filter in Timber I need to create a filter function that uses Twig_SimpleFilter and does that sorting on the repeater field array.

我已经测试过,答案中的示例 rot13 过滤器适用于 {{'test text'| rot13}}

I've tested that, and the sample rot13 filter from that answer works on {{ 'test text'|rot13 }}.

但是当尝试将相同的 Twig_SimpleFilter 结构与代码以日期对数组进行排序,即在主题的 functions.php 文件中使用以下代码:

But when trying to use the same Twig_SimpleFilter structure with the code to sort the array by date, i.e. using this in my theme's functions.php file:

add_filter('timber/twig', function($twig) {
   $twig->addExtension(new Twig_Extension_StringLoader());

   $twig->addFilter(
     new Twig_SimpleFilter(
       'sort_timeline', 
       function($string) {

$repeater = get_field('timeline');

$order = array();

foreach( $repeater as $i => $row ) {
    $order[ $i ] = $row['date'];
}

array_multisort( $order, SORT_DESC, $repeater );

       }
     )
   );

   return $twig;
});

并像这样调用过滤器 {%for i,in repeater |在 view.twig

{% set repeater = timeline %}

       {% for i, row in repeater|sort_timeline %}

          {{ row.date}}

          {{ row.description}}

      {% endfor %}

我得到的只是一个白屏,并且在php日志中没有错误。

all I get is a whitescreen and no errors in the php log.

FWIW,使用 rot13 {%代表i,在转发器中的行| rot13%} 这样的过滤器也会显示白屏,因此总体 Twig_SimpleFilter出了问题

FWIW, using the rot13 filter like {% for i, row in repeater|rot13 %} also shows a whitescreen, so something is wrong with the overall Twig_SimpleFilter.

(顺便说一句,我也意识到我可能需要将php日期格式转换为 $ row ['date'] 正确排序,也许使用 strtotime ,因为它现在只是月份和年份。)

(As an aside, I also realize that I may need to convert the php date format in $row['date'] to correctly sort, maybe using strtotime, since it is now simply month and year.)

在Twig中使用过滤器是正确的方法吗?或者是否可以直接在 .twig array_multisort($ order,SORT_DESC,$ repeater); 函数c>模板?

Is using a filter in Twig the right way to try this? Or is it possible to adapt and use the array_multisort( $order, SORT_DESC, $repeater ); function directly in the .twig template?

编辑4/17/18

@ num8er的代码适用于php5 +。

此外,下面的另一个功能将对转发器的后端行进行排序;更改值并将其添加到主题的 functions.php 文件中。参见 https:// support.advancedcustomfields.com/forums/topic/sort-repeater-in-back-end-where-data-is-entered/

And, this other function below will sort the backend rows of the repeater; change values and add to the theme's functions.php file. See https://support.advancedcustomfields.com/forums/topic/sort-repeater-in-back-end-where-data-is-entered/

要排序的行的MySQL field_123456789 名称,转到ACF导出页面并导出字段组。 https://support.advancedcustomfields.com / forums / topic / how-to-to-retrieve-a-group-field-key /

To get the MySQL field_123456789 name of the row you want to sort on, go to the ACF export page and export the field group. https://support.advancedcustomfields.com/forums/topic/how-to-retrieve-a-group-field-key/

add_filter('acf/load_value/name=timeline', 'my_acf_load_value', 10, 3); //repeater name is timeline
function my_acf_load_value( $rows)
{
 foreach( $rows as $key => $row ) {
  $column_id[ $key ] = $row['field_5967e7639a09b'];

 }

 array_multisort( $column_id, SORT_DESC, $rows );
 return $rows;
}


推荐答案

直接解决您的问题。

尝试以下代码:

add_filter('timber/twig', function($twig) {
   $twig->addExtension(new Twig_Extension_StringLoader());

   $twig->addFilter(
     new Twig_SimpleFilter(
       'timeline_latest_first', 
       function($timeline) {

         usort($timeline, function($a, $b) {
           if(strtotime($a['date']) === strtotime($b['date'])) return 0;
           return strtotime($a['date']) > strtotime($b['date']) ? -1 : 1; 
           // or simply (if php 7.x):
           // return -1*(strtotime($a['date']) <=> strtotime($b['date']));
         });

         return $timeline;
       }
     )
   );

   return $twig;
});


{% set timeline_sorted = timeline|timeline_latest_first %}

{% for i, row in timeline_sorted %}

  {{ row.date}}

  {{ row.description}}

{% endfor %}

这篇关于从PHP和Timber / Twig中的“高级自定义字段”对转发器字段进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆