Skip to content

Commit

Permalink
fields.php
Browse files Browse the repository at this point in the history
updated README.md
  • Loading branch information
Jan Horecny committed Aug 23, 2017
1 parent 2547286 commit d89af5b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ Options::get('your_options_key', 'language_code');
*3. Get option value from default (primary) language*
```
Options::get_default('your_options_key');
```

## fields.php
Load specific field values at onces (with one function call). Ideal when you don`t want to load all fields using get_fields() function.

**Usage:**
*1. Get values for specific set of custom fields*
```
$fields = get_custom_fields(['field1', 'field2']);
```
33 changes: 33 additions & 0 deletions fields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Return values as key => value paired array based on specific field names
*
* Example: $fields = get_custom_fields(['field1', 'field2']);
*
* @version 1.0.0
* @link https://www.advancedcustomfields.com/resources/get_field/
* @author Jan Horecny <[email protected]>
* @todo add support for post IDs
*
* @param bool|array $fields
* @param bool $includeBlank
*
* @return bool|array
*/
function get_custom_fields( $fields = false, $includeBlank = true ) {
if ( ! is_array( $fields ) ) {
return false;
}

$values = false;

foreach ( $fields as $field ) {
if ( get_field( $field ) ) {
$values[ $field ] = get_field( $field );
} elseif ( $includeBlank ) {
$values[ $field ] = '';
}
}

return $values;
}

0 comments on commit d89af5b

Please sign in to comment.