ACF & PHP-API (WordPress): verschil tussen versies
Naar navigatie springen
Naar zoeken springen
Regel 4: | Regel 4: | ||
To start with some bad news: While ACF fields are stored in tables <code>wp_options</code> or <code>wp_postmeta</code>, you cannot write directly to these tables to create ACF fields. Then the 'context' is missing, eg a repeater. So you have to do it via an API call. | To start with some bad news: While ACF fields are stored in tables <code>wp_options</code> or <code>wp_postmeta</code>, you cannot write directly to these tables to create ACF fields. Then the 'context' is missing, eg a repeater. So you have to do it via an API call. | ||
+ | |||
+ | == Two kinds of fields == | ||
+ | |||
+ | There seems to be a distinction between two kinds of ACF fields and how to approach them: | ||
+ | |||
+ | * Fields that are associated with specific posts | ||
+ | * Fiels that are ''not'' asssociated with specific posts. | ||
+ | |||
+ | == Retrieving a post-related field == | ||
+ | |||
+ | Simple example: The home page of a site has ID=7. It has a custom field ''hero text''. How to fetch its value: | ||
+ | |||
+ | <pre> | ||
+ | <?php | ||
+ | |||
+ | require_once("/var/www/example.com/wp-load.php"); | ||
+ | |||
+ | the_field("hero_text",7); | ||
+ | </pre> | ||
+ | |||
+ | Output: | ||
+ | |||
+ | <pre> | ||
+ | Eat more chips! | ||
+ | </pre> | ||
+ | |||
+ | * Function <code>[[The field (ACF) | the_field()]]</code> displays the value of a field directly | ||
+ | * Alternatively, function <code>[[Get field() (ACF) | get_field()]] returns the value rather than directly displaying it. | ||
== See also == | == See also == |
Versie van 1 aug 2022 10:18
How can you automatically manage ACF fields? In particular, fill in, delete and edit?
Not directly at database level
To start with some bad news: While ACF fields are stored in tables wp_options
or wp_postmeta
, you cannot write directly to these tables to create ACF fields. Then the 'context' is missing, eg a repeater. So you have to do it via an API call.
Two kinds of fields
There seems to be a distinction between two kinds of ACF fields and how to approach them:
- Fields that are associated with specific posts
- Fiels that are not asssociated with specific posts.
Simple example: The home page of a site has ID=7. It has a custom field hero text. How to fetch its value:
<?php require_once("/var/www/example.com/wp-load.php"); the_field("hero_text",7);
Output:
Eat more chips!
- Function
the_field()
displays the value of a field directly - Alternatively, function
get_field() returns the value rather than directly displaying it.
See also
Sources
- https://www.advancedcustomfields.com/resources/
- https://www.advancedcustomfields.com/resources/#functions - All PHP functions together. It's not complete, though!
- https://www.advancedcustomfields.com/resources/code-examples/