ACF PHP 一个框架用于使用PHP注册ACF字段
ACF PHP 一个框架用于使用PHP注册ACF字段
ACF PHP
A framework to register Advanced Custom Fields fields using PHP.
Why use ACF PHP?
ACF PHP makes it easier to register ACF fields using PHP by automating the tedious parts of adding new fields and adding defaults that make sense.
Without ACF PHP:
array ( 'key' => 'field_random_key', 'label' => 'Header Title', 'name' => 'header_title', 'type' => 'text',),array ( 'key' => 'field_another_random_key', 'label' => 'Header Subtitle', 'name' => 'header_subtitle', 'type' => 'text',),array ( 'key' => 'field_yet_another_random_key', 'label' => 'Header Background Image', 'name' => 'header_bg_image', 'type' => 'image',)
'location' => array( array( array( 'param' => 'post_type', 'operator' => '==', 'value' => 'page', ), ),),
With ACF PHP:
'header_title','header_subtitle''header_bg_image' => array( 'label' => 'Header Background Image', 'type' => 'image',)
'location' => 'post_type == page',
Features
Automatically generates a unique key for each field using the field group key and field name.Adds a label automatically if you don’t add one using the field name, and adds a default field type (example: header_title becomes a Text field with label Header Title).Easily reuse fields by putting them in a variable.Options to hide the "Custom Fields" menu (including redirects when accessed directly) and to disable ACF on the front end (useful when using native WordPress function to retrieve post meta, eg. get_post_meta()).All ACF fields are supported, including Repeaters and Flexible Content.
Usage
Download the .zip file from GitHub to install in your WordPress admin.
All options for ACF fields are supported. For more information, please check out the official Advanced Custom Fields website.
Basic example:
Setting up the metabox:
$page_settings_metabox = array( 'key' => 'page_settings', // make sure the key is unique for each group 'title' => 'Page Settings', 'location' => array( array( array( 'param' => 'post_type', 'operator' => '==', 'value' => 'page', ), ), ), 'fields' => array( 'text' => array( 'required' => 1, ), 'textarea' => array( 'type' => 'textarea', 'rows' => 5, 'instructions' => 'Add optional field instructions.', ), ),);
Register the metabox:
if ( function_exists( 'acf_php_add_local_field_group' ) ) { acf_php_add_local_field_group( $page_settings_metabox );}
Retrieve the meta values:
$text_content = get_post_meta( get_the_ID(), 'text', true );$textarea_content = get_post_meta( get_the_ID(), 'textarea', true );
Alternatively, use standard ACF functions.
Repeater example:
'fields' => array( 'repeater' => array( 'type' => 'repeater', 'button_label' => 'Add Row', 'sub_fields' => array( 'text' => array( 'required' => 1, ), 'textarea' => array( 'type' => 'textarea', 'rows' => 5, 'instructions' => 'Add optional field instructions.', ), ), ),),
Flexible Content example:
'fields' => array( 'flexible_content' => array( 'type' => 'flexible_content', 'button_label' => 'Add Section', 'layouts' => array( 'example_layout' => array( 'sub_fields' => array( 'text_field', 'true_false_field' => array( 'type' => 'true_false', ), ), ), ), ),),
Reusable field example:
$checkbox = array( 'type' => 'checkbox', 'choices' => array( 'red' => 'Red', 'white' => 'White', 'blue' => 'Blue', ),);'fields' => array( 'checkbox_field' => $checkbox,),
To-do
Improve ACF_PHP_Metabox class (leaner code).Add support for multiple fields in a single variable.Improve field group location settings.
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~