【问题标题】:Output Fields Of Entire Group Advanced Custom Fields整个组的输出字段高级自定义字段
【发布时间】:2014-07-09 19:58:30
【问题描述】:

我正在尝试找到一种方法(使用 wordpress 插件 ACF)来输出整个字段组。

我有两个“字段组”,我想说它们中的任何一个是否为“活动”(带有复选框)以将它们回显到我的模板。

问题是,如何从一个“字段组”中获取所有字段数据的数组?

现在我正在尝试以下操作:

<?php

$group_ID = 37;

$fields = array();
$fields = apply_filters('acf/field_group/get_fields', $fields, $group_ID);

echo "<pre>";
print_r($fields);
echo "</pre>";

?>

它输出一个字段数组,但据我所知,它不包含实际数据:

Array
(
    [0] => Array
        (
            [key] => field_537b79d92081f
            [label] => Active-1
            [name] => active-1
            [_name] => active-1
            [type] => true_false
            [order_no] => 0
            [instructions] => 
            [required] => 0
            [id] => acf-field-active-1
            [class] => true_false
            [conditional_logic] => Array
                (
                    [status] => 0
                    [rules] => Array
                        (
                            [0] => Array
                                (
                                    [field] => null
                                    [operator] => ==
                                )

                        )

                    [allorany] => all
                )

            [message] => 
            [default_value] => 0
            [field_group] => 37
        )

    [1] => Array
        (
            [key] => field_537b78a4c743e
            [label] => Title-1
            [name] => title-1
            [_name] => title-1
            [type] => text
            [order_no] => 1
            [instructions] => 
            [required] => 0
            [id] => acf-field-title-1
            [class] => text
            [conditional_logic] => Array
                (
                    [status] => 0
                    [rules] => Array
                        (
                            [0] => Array
                                (
                                    [field] => field_537b79d92081f
                                    [operator] => ==
                                    [value] => 1
                                )

                        )

                    [allorany] => all
                )

            [default_value] => 
            [placeholder] => 
            [prepend] => 
            [append] => 
            [formatting] => html
            [maxlength] => 
            [field_group] => 37
        )

    [2] => Array
        (
            [key] => field_537b78c6c743f
            [label] => Info-1
            [name] => info-1
            [_name] => info-1
            [type] => textarea
            [order_no] => 2
            [instructions] => 
            [required] => 0
            [id] => acf-field-info-1
            [class] => textarea
            [conditional_logic] => Array
                (
                    [status] => 0
                    [rules] => Array
                        (
                            [0] => Array
                                (
                                    [field] => field_537b79d92081f
                                    [operator] => ==
                                    [value] => 1
                                )

                        )

                    [allorany] => all
                )

            [default_value] => 
            [placeholder] => 
            [maxlength] => 
            [rows] => 
            [formatting] => br
            [field_group] => 37
        )

)

我想基本上做以下事情(请原谅 sudo/损坏的代码,PHP 不是我的语言):

<?php

    $group_ID1 = 37;

    $fields = array($group_ID1);

    if ($fields1['active'] !== null) {
        echo "<h1>";
        echo $fields1['active']['text'];
        echo $fields1['title']['text'];
        echo $fields1['content']['text'];
        echo "<h1>";
    }

    $group_ID2 = 38;

    $fields2 = array($group_ID2);

    if ($fields2['active'] !== null) {
        echo "<h1>";
        echo $fields2['active']['text'];
        echo $fields2['title']['text'];
        echo $fields2['content']['text'];
        echo "<h1>";
    }

?>

我还可以获得如下特定字段数据:

<?php

    $field_key = "field_537b78c6c743f";
    $field = get_field_object($field_key);

    if($field) {
        echo "<pre>";
        print_r($field);
        echo "</pre>";
    }

?>

输出如下:

Array
(
    [key] => field_537b78c6c743f
    [label] => Info-1
    [name] => info-1
    [_name] => info-1
    [type] => textarea
    [order_no] => 2
    [instructions] => 
    [required] => 0
    [id] => acf-field-info-1
    [class] => textarea
    [conditional_logic] => Array
        (
            [status] => 0
            [rules] => Array
                (
                    [0] => Array
                        (
                            [field] => field_537b79d92081f
                            [operator] => ==
                            [value] => 1
                        )

                )

            [allorany] => all
        )

    [default_value] => 
    [placeholder] => 
    [maxlength] => 
    [rows] => 
    [formatting] => br
    [field_group] => 37
    [value] => testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest
)

但是我真的需要为每个“字段组”调用每个字段键吗?一个“字段组”中没有所有字段的数组吗?

【问题讨论】:

    标签: php wordpress custom-fields advanced-custom-fields


    【解决方案1】:

    我使用以下方法得到了我想要的:

    <?php
        // Get wordpress page all fields
        $post_id = get_the_ID();
        $fields = get_field_objects($post_id);
    ?>
    
    <?php
        // Remember Widget
        $status = $fields['remember-status']['value'];
        $text = $fields['remember-text']['value'];
        if ($status == TRUE) {
            echo "<div class=\"info-container\">";
                echo "<p>" . $text . "</p>";
            echo "</div>";
        };
    ?>
    

    【讨论】:

      【解决方案2】:

      您可以获得属于一个组的所有字段的数组。功能是:

      $acf_fields = acf_get_fields_by_id( $group_ID );
      

      我们需要知道 ACF 字段组 ID。您可以在群组编辑页面的 url 中找到它,例如:

      http://site.ru/wp-admin/post.php?post=340&action=edit
      

      在这种情况下,组 ID 是 340。如果您不想使用硬编码 ID(如果您的组不时更改),您可以使用组名获取它(在此示例中组名 id Technic CPT ):

      global $wpdb;
      $group_ID = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = 'Technic CPT'" );
      

      输出将如下所示:

      array(1) {
        [0] => array(29) {
          ["ID"] => int(366)
          ["key"] => string(19) "field_55f05295a59ca"
          ["label"] => string(29) "Gallery of technic"
          ["name"] => string(15) "technic_gallery"
          ["prefix"] => string(0) ""
          ["type"] => string(7) "gallery"
          ["value"] => NULL
          ["menu_order"] => int(6)
          ["instructions"] => string(155) "Field instructions"
          ["required"] => int(0)
          ["id"] => string(0) ""
          ["class"] => string(0) ""
          ["conditional_logic"] => int(0)
          ["parent"] => int(340)
          ["wrapper"] => array(3) {
            ["width"] => int(50)
            ["class"] => string(0) ""
            ["id"] => string(0) ""
          }
          ["_name"] => string(15) "technic_gallery"
          ["_input"] => string(0) ""
          ["_valid"] => int(1)
          ["min"] => string(0) ""
          ["max"] => int(10)
          ["preview_size"] => string(9) "thumbnail"
          ["library"] => string(3) "all"
          ["min_width"] => int(475)
          ["min_height"] => int(365)
          ["min_size"] => string(0) ""
          ["max_width"] => string(0) ""
          ["max_height"] => string(0) ""
          ["max_size"] => int(2)
          ["mime_types"] => string(0) ""
        }
      }
      

      【讨论】:

      • ACF 4.4.7(当前免费版本)中不再(还?)提供此功能
      • 是的,我需要提一下:我使用的是pro 版本的acf。我将查看替代代码,感谢您指出这一点!
      【解决方案3】:

      过滤器acf/field_group/get_fields 似乎没有加载字段。

      您必须在获取它们后加载它们,但最好在尝试加载它们之前检查值是否已设置,以防发生任何变化:)

      您可以使用acf/load_value 和可能的acf/format_value 过滤器来获得所需的输出。

      我创建了这个小加载器函数,它引用到由acf/field_group/get_fields 过滤器获得的 ACF 字段数组,循环通过它们并在必要时加载(和格式化)它们的值:

      function my_load_ACF_fields_values( &$ACF_FIELDS, $post_id = null ){
        if( empty($ACF_FIELDS) ) return false;
        if( empty($post_id) ) $post_id = get_the_ID();
      
        foreach( $ACF_FIELDS as &$acf_field ){
          if( !isset($acf_field['value']) ){
             $acf_field['value'] = apply_filters('acf/load_value', false, $post_id, $acf_field);
             $acf_field['formatted_value'] = apply_filters(
                'acf/format_value', $acf_field['value'], $post_id, $acf_field
             );
          }
        } 
        return true;
      }
      

      所以用法如下:

      //1. GET FIELDS BY GROUP ID
      $my_acf_fields = apply_filters('acf/field_group/get_fields', [], $my_acf_GROUP_ID);
      
      //2. LOAD/INJECT THEIR VALUES
      my_load_ACF_fields_values( $my_acf_fields /*, $possiblyCustomPostID */ );
      
      //3. Display/Process fields...
      

      注意:请注意,我知道逐个加载值并不是一个好主意,遗憾的是 ACF 的 api 似乎没有更好的方法,而且最重要的是该插件自己为 ACF 的本机显示执行此操作...

      【讨论】:

        猜你喜欢
        • 2017-12-16
        • 1970-01-01
        • 2018-08-06
        • 2013-11-25
        • 2015-01-31
        • 1970-01-01
        • 2018-08-26
        • 2013-07-14
        • 2012-08-10
        相关资源
        最近更新 更多