It’s easy to get the values stored by Simple Fields. Most commonly used are these functions:
- simple_fields_value()
retrieve one or several fields values from a post - simple_fields_values()
retrieve one or several repeatable field values from a post - simple_fields_fieldgroup()
The easiest way to get all field values from a field group. Depending on if the field group is repeatable or not, simple_fields_value or simple_fields_values will be used.
Read more about them on their respective page, or read the quick overview below.
Gettings values from non-repeatable fields
Use the function simple_fields_value()
to get value(s) from repeatable fields.
The function can take 3 arguments:
- the field slug(s) to get the value for
- the post to get the values from (optional, defaults to the current post in the loop)
- options to send to field type (as array or query string).
If you have a simple text field with the slug “myFieldSlug” you can get the value from that field like this:
$field_value = simple_fields_value("myFieldSlug");
echo "The field has the value: $field_value";
You can also get the values from several fields at once: just add all the fields to the same function as a comma separated list:
$field_values = simple_fields_value("myFieldSlug,anotherFieldSlug,aThirdFieldSlug");
echo "The first field has the value:" . $field_values["myFieldSlug"];
echo "The second field has the value:" . $field_values["anotherFieldSlug"];
echo "The third field has the value:" . $field_values["aThirdFieldSlug"];
The difference between getting one field and several fields is that if you get only one field its return value gets returned directly, while getting several fields means that you get an array in return instead, with each field stored in a key with it’s slug as the key.
Getting values from repeatable fields
Use the function simple_fields_values() to get values from a field that belongs to a repeatable field group. This function uses the same arguments as somple_fields_value(). The only difference is the return value that always is an array.
$repeatable_field_values = simple_fields_values("slug_image,slug_description");
foreach ($repeatable_field_values as $values) {
echo "image has id: " . $values["slug_image"] . " and description " . $values["slug_description"]
}
Getting values from a whole group of fields
With simple_fields_fieldgroup you can get all values from all fields in a fieldgroup, no matter if the field group is repeatable or not.
I need add a fields in quick(inline) edit form. Anyhelp?
Hi, great plugin but it’s not recognising short codes when it pulls data from the HTML-editor textarea. Is there something I can do to fix this?
try
echo apply_filters("the_content", $the_text_you_got_from_simple_fields)
Yes! Thank you Pär, that worked! You are a gentleman and a scholar, I doff my hat to you.
I get only id but not able to show image from repeating fields. plz help me..
The output give a 1. How do i change the output ‘1’ into the field name?
code:
Hi harry,
the comments here does not work so well with code.
Could you ask your question over at the support forum at wordpress instead?
http://wordpress.org/support/plugin/simple-fields
thanks!
If I don’t know hot to create PHP functions – can you show me the whole code to past?
@jacek: not quite sure exactly what you need help with. Could you write a more detailed support question over at the Support Forum?
The site I’m working on doesn’t give me the option to choose a slug using Simple Fields and the Show Custom Field Keys gives me the key but not the slug. I thought I saw a slug option once but can’t find it again for the life of me.
Do you have the latest version? Slugs where added in version 1 I think, really don’t remember exactly when 🙂
You enter the slugs in Settings > Simple Fields > Your field group
How do you know what the slug is?
When you define/add the fields you also enter the slug of each field. If you already have added slugs but forgot what you called them you can view them on the post edit screen using the “Show custom field keys”-link.