📜  taxonomy acf - PHP (1)

📅  最后修改于: 2023-12-03 15:20:28.855000             🧑  作者: Mango

Taxonomy ACF

Taxonomy ACF is a PHP library that allows developers to easily add custom fields to taxonomies in WordPress. This library is built on top of Advanced Custom Fields (ACF), which is a popular custom fields plugin for WordPress.

Installation

To install Taxonomy ACF, you can use Composer or download the library manually. Here is an example of how to install it using Composer:

composer require johnbillion/taxonomy-acf
Usage

To use Taxonomy ACF, you first need to create a field group using the ACF plugin. This field group should contain all of the custom fields that you want to add to the taxonomy. Once you have created the field group, you can use the register_taxonomy_acf_fields() function to register the custom fields for a specific taxonomy.

Here is an example of how to register custom fields for a taxonomy:

function register_custom_taxonomy() {
    // Register the taxonomy first
    register_taxonomy( 'my_taxonomy', 'my_post_type', array(
        'labels' => array(
            'name' => __( 'My Taxonomy' ),
            'singular_name' => __( 'My Taxonomy' ),
        ),
        'public' => true,
        'rewrite' => array( 'slug' => 'my_taxonomy' ),
    ) );
 
    // Register the custom fields using Taxonomy ACF
    register_taxonomy_acf_fields( 'my_taxonomy', array(
        array(
            'key' => 'field_1',
            'label' => 'Field 1',
            'name' => 'field_1',
            'type' => 'text',
        ),
        array(
            'key' => 'field_2',
            'label' => 'Field 2',
            'name' => 'field_2',
            'type' => 'textarea',
        ),
    ) );
}
add_action( 'init', 'register_custom_taxonomy' );

This code will register a custom taxonomy called "My Taxonomy" with two custom fields called "Field 1" and "Field 2".

Conclusion

Taxonomy ACF is a powerful tool for developers who want to add custom fields to taxonomies in WordPress. By using this library, you can easily create custom fields for your taxonomies without having to write a lot of custom code.