ACF - get fields from group

时间:2013-04-09 作者:T.Todua

我一直在使用此代码获取特定组中的所有字段:

<?php
$GroupOrPostSlug = \'acf_specialgroup\';
//or insert the ID of your fields Group.
$groupID=\'\';



global $wpdb;
if (empty($groupID))
{$groupID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name =  \'$GroupOrPostSlug\' ");}
if (empty($groupID))
{$groupID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title =  \'$GroupOrPostSlug\' ");}

$custom_field_keys = get_post_custom_keys($groupID);
foreach ( $custom_field_keys as $key => $fieldkey )
{
    if (stristr($fieldkey,\'field_\'))
    {
        //echo $key . " => " . $fieldkey . "<br />";
        //echo $field[\'label\'] . \': \' . $field[\'value\'];
        //echo $fieldkey . "<br />";
        $field = get_field_object($fieldkey, $groupID);
        echo $field[\'label\'];

    }
}
?>
但它只显示字段的名称。难道不存在这样一个简单的挂钩/函数吗:get_all_fields_from_group($id)

6 个回复
SO网友:Dejan

非常感谢您的帖子,我花了半天时间研究如何按组获取字段名。

如果您有字段名,则可以轻松获取其值:get\\u field($field[\'name]);

EXAMPLE HOW TO GET IMAGES FOR SLIDER

    <?php 
                    //or insert the ID of your fields Group.
                    $groupID=\'116\';
                    $custom_field_keys = get_post_custom_keys($groupID);
                    foreach ( $custom_field_keys as $key => $fieldkey )
                    {
                        if (stristr($fieldkey,\'field_\'))
                        {
                            //echo $key . " => " . $fieldkey . "<br />";
                            //echo $field[\'label\'] . \': \' . $field[\'value\'];
                            //echo $fieldkey . "<br />";
                            $field = get_field_object($fieldkey, $groupID); 

                            $acf_field_name = $field[\'name\'];

                            $attachment = get_field($acf_field_name);
                            echo "<img src=\'".$attachment[\'url\']."\' title=\'".$attachment[\'title\']."\'/>";


                        }
                    }

?> 
再次感谢!

SO网友:Lucas Bustamante

我很惊讶没有人提到acf_get_fields(\'someGroupId\') 然而

/**
 * acf_get_fields
 *
 * Returns and array of fields for the given $parent.
 *
 * @date    30/09/13
 * @since   5.0.0
 *
 * @param   (int|string|array) $parent The field group or field settings. Also accepts the field group ID or key.
 * @return  array
 */

SO网友:Alexander Wallin

我创建了一个函数来获取组的字段:

function my_acf_get_fields_in_group( $group_id ) {
    $acf_meta = get_post_custom( $group_id );
    $acf_fields = array();

    foreach ( $acf_meta as $key => $val ) {
        if ( preg_match( "/^field_/", $key ) ) {
            $acf_fields[$key] = $val;
        }
    }

    return $acf_fields;
}

SO网友:ionfish

如果有人像我一样偶然发现了这个页面,并试图弄明白这一点,这些方法将不再适用于5版及以上的高级自定义字段,因为它使用的是posts而不是POSTETA。

在这种环境中,我可以通过这种方式从组中获取字段,并将其用作选定字段的值:

function acf_load_select_choices( $field ) {

  global $wpdb;
  $group_slug = \'Name or slug of the group\';

  $group_ID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name =  \'$group_slug\' "); 
  if (empty($group_ID))
    $group_ID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title =  \'$group_slug\' ");

  $field[\'choices\'] = array();
  $fields = acf_get_fields_by_id($group_ID);

  // to see what $fields contains, use:
  echo \'<pre>\'.var_dump($fields).\'</pre>\';

  if( $fields ) {
    foreach( $fields as $fieldrow ) {
      $field[\'choices\'][ $fieldrow[\'name\'] ] = $fieldrow[\'label\'];
    }
  }

  return $field;

}

add_filter(\'acf/load_field/name=selectfieldname\', \'acf_load_select_choices\');

SO网友:Luca Reghellin

由于ACF使用“ACF”类型的自定义帖子作为组,因此我们可以使用get\\u page\\u by\\u title检索组的ID,然后执行其余操作。下面是一个小的实用函数:

// This was tested with ACF free edition v4.4.11
function get_group_fields($group_name){
  $group = get_page_by_title($group_name, OBJECT, \'acf\');
  if(empty($group)) return false;

  $meta = get_post_meta($group->ID);
  $acf_fields = array();

  foreach($meta as $key => $value){
    $acf_meta_key = stristr($key,\'field_\'); // acf fields all start with "field_"
    if($acf_meta_key) $acf_fields[] = get_field_object($key);
  }

  return $acf_fields; // returns an array of field objects
}
用法:

$group_fields = get_group_fields(\'My marvelous group\');

foreach($group_fields as $field){
   $label = $field[\'label\'];
   $value = $field[\'value\'];
   // etc...
}

SO网友:Artemiy Egorov

如果有人仍在搜索。您可以这样做:

<?php 

      $fields = get_field_objects();

      if( $fields )
      {
        foreach( $fields as $field_name => $field )
        {
            echo \'<div>\';
                echo \'<h3>\' . $field[\'label\'] . \'</h3>\';
                echo $field[\'value\'];
            echo \'</div>\';
        }
      }
?>

结束

相关推荐

ACF - get fields from group - 小码农CODE - 行之有效找到问题解决它

ACF - get fields from group

时间:2013-04-09 作者:T.Todua

我一直在使用此代码获取特定组中的所有字段:

<?php
$GroupOrPostSlug = \'acf_specialgroup\';
//or insert the ID of your fields Group.
$groupID=\'\';



global $wpdb;
if (empty($groupID))
{$groupID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name =  \'$GroupOrPostSlug\' ");}
if (empty($groupID))
{$groupID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title =  \'$GroupOrPostSlug\' ");}

$custom_field_keys = get_post_custom_keys($groupID);
foreach ( $custom_field_keys as $key => $fieldkey )
{
    if (stristr($fieldkey,\'field_\'))
    {
        //echo $key . " => " . $fieldkey . "<br />";
        //echo $field[\'label\'] . \': \' . $field[\'value\'];
        //echo $fieldkey . "<br />";
        $field = get_field_object($fieldkey, $groupID);
        echo $field[\'label\'];

    }
}
?>
但它只显示字段的名称。难道不存在这样一个简单的挂钩/函数吗:get_all_fields_from_group($id)

6 个回复
SO网友:Dejan

非常感谢您的帖子,我花了半天时间研究如何按组获取字段名。

如果您有字段名,则可以轻松获取其值:get\\u field($field[\'name]);

EXAMPLE HOW TO GET IMAGES FOR SLIDER

    <?php 
                    //or insert the ID of your fields Group.
                    $groupID=\'116\';
                    $custom_field_keys = get_post_custom_keys($groupID);
                    foreach ( $custom_field_keys as $key => $fieldkey )
                    {
                        if (stristr($fieldkey,\'field_\'))
                        {
                            //echo $key . " => " . $fieldkey . "<br />";
                            //echo $field[\'label\'] . \': \' . $field[\'value\'];
                            //echo $fieldkey . "<br />";
                            $field = get_field_object($fieldkey, $groupID); 

                            $acf_field_name = $field[\'name\'];

                            $attachment = get_field($acf_field_name);
                            echo "<img src=\'".$attachment[\'url\']."\' title=\'".$attachment[\'title\']."\'/>";


                        }
                    }

?> 
再次感谢!

SO网友:Lucas Bustamante

我很惊讶没有人提到acf_get_fields(\'someGroupId\') 然而

/**
 * acf_get_fields
 *
 * Returns and array of fields for the given $parent.
 *
 * @date    30/09/13
 * @since   5.0.0
 *
 * @param   (int|string|array) $parent The field group or field settings. Also accepts the field group ID or key.
 * @return  array
 */

SO网友:Alexander Wallin

我创建了一个函数来获取组的字段:

function my_acf_get_fields_in_group( $group_id ) {
    $acf_meta = get_post_custom( $group_id );
    $acf_fields = array();

    foreach ( $acf_meta as $key => $val ) {
        if ( preg_match( "/^field_/", $key ) ) {
            $acf_fields[$key] = $val;
        }
    }

    return $acf_fields;
}

SO网友:ionfish

如果有人像我一样偶然发现了这个页面,并试图弄明白这一点,这些方法将不再适用于5版及以上的高级自定义字段,因为它使用的是posts而不是POSTETA。

在这种环境中,我可以通过这种方式从组中获取字段,并将其用作选定字段的值:

function acf_load_select_choices( $field ) {

  global $wpdb;
  $group_slug = \'Name or slug of the group\';

  $group_ID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name =  \'$group_slug\' "); 
  if (empty($group_ID))
    $group_ID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title =  \'$group_slug\' ");

  $field[\'choices\'] = array();
  $fields = acf_get_fields_by_id($group_ID);

  // to see what $fields contains, use:
  echo \'<pre>\'.var_dump($fields).\'</pre>\';

  if( $fields ) {
    foreach( $fields as $fieldrow ) {
      $field[\'choices\'][ $fieldrow[\'name\'] ] = $fieldrow[\'label\'];
    }
  }

  return $field;

}

add_filter(\'acf/load_field/name=selectfieldname\', \'acf_load_select_choices\');

SO网友:Luca Reghellin

由于ACF使用“ACF”类型的自定义帖子作为组,因此我们可以使用get\\u page\\u by\\u title检索组的ID,然后执行其余操作。下面是一个小的实用函数:

// This was tested with ACF free edition v4.4.11
function get_group_fields($group_name){
  $group = get_page_by_title($group_name, OBJECT, \'acf\');
  if(empty($group)) return false;

  $meta = get_post_meta($group->ID);
  $acf_fields = array();

  foreach($meta as $key => $value){
    $acf_meta_key = stristr($key,\'field_\'); // acf fields all start with "field_"
    if($acf_meta_key) $acf_fields[] = get_field_object($key);
  }

  return $acf_fields; // returns an array of field objects
}
用法:

$group_fields = get_group_fields(\'My marvelous group\');

foreach($group_fields as $field){
   $label = $field[\'label\'];
   $value = $field[\'value\'];
   // etc...
}

SO网友:Artemiy Egorov

如果有人仍在搜索。您可以这样做:

<?php 

      $fields = get_field_objects();

      if( $fields )
      {
        foreach( $fields as $field_name => $field )
        {
            echo \'<div>\';
                echo \'<h3>\' . $field[\'label\'] . \'</h3>\';
                echo $field[\'value\'];
            echo \'</div>\';
        }
      }
?>

相关推荐