星期三 , 22 1 月 2025

carbon fields 示范代码 (带tabs)

<?php  

use Carbon_Fields\Container;
use Carbon_Fields\Field;

// Boot Carbon Fields
add_action('after_setup_theme', 'crb_load');
function crb_load() {
    \Carbon_Fields\Carbon_Fields::boot();
}

// Create a Post Meta container
add_action('carbon_fields_register_fields', 'create_post_meta');
function create_post_meta() {
    Container::make('post_meta', 'Additional Details')
        ->where('post_type', '=', 'post')
        ->add_fields(array(
            Field::make('text', 'crb_subtitle', 'Subtitle')
                ->set_help_text('Add a subtitle for your post'),
            
            Field::make('rich_text', 'crb_summary', 'Summary')
                ->set_required(true),
            
            Field::make('image', 'crb_featured_image_2', 'Secondary Featured Image')
                ->set_value_type('id'),
            
            Field::make('complex', 'crb_gallery', 'Image Gallery')
                ->add_fields(array(
                    Field::make('image', 'image', 'Image'),
                    Field::make('text', 'caption', 'Caption')
                ))
                ->set_layout('tabbed-horizontal')
        ));

     Container::make('theme_options', __('Theme Settings'))
        ->add_tab(__('General Settings'), array(
            // Basic Information Section
            Field::make('separator', 'crb_basic_info_separator', __('Basic Information'))
                ->set_classes('separator-class'),
            Field::make('text', 'crb_site_tagline', __('Site Tagline'))
                ->set_help_text('Enter your site tagline'),
            Field::make('textarea', 'crb_site_description', __('Site Description')),
            
            // Contact Information Section
            Field::make('separator', 'crb_contact_separator', __('Contact Information'))
                ->set_classes('separator-class'),
            Field::make('text', 'crb_phone', __('Phone Number')),
            Field::make('text', 'crb_email', __('Email Address')),
            Field::make('text', 'crb_address', __('Physical Address'))
        ))
        
        ->add_tab(__('Social Media'), array(
            // Social Media Links Section
            Field::make('separator', 'crb_social_separator', __('Social Media Links'))
                ->set_classes('separator-class'),
            Field::make('text', 'crb_facebook', __('Facebook URL')),
            Field::make('text', 'crb_twitter', __('Twitter URL')),
            Field::make('text', 'crb_instagram', __('Instagram URL')),
            Field::make('text', 'crb_linkedin', __('LinkedIn URL')),
            
            // Social Media Settings
            Field::make('separator', 'crb_social_settings_separator', __('Social Media Settings'))
                ->set_classes('separator-class'),
            Field::make('checkbox', 'crb_show_social_header', __('Show in Header'))
                ->set_option_value('yes'),
            Field::make('checkbox', 'crb_show_social_footer', __('Show in Footer'))
                ->set_option_value('yes'),
            Field::make('select', 'crb_social_icon_size', __('Icon Size'))
                ->add_options(array(
                    'small' => 'Small',
                    'medium' => 'Medium',
                    'large' => 'Large'
                ))
        ))
        
        ->add_tab(__('Header Options'), array(
            // Logo Settings
            Field::make('separator', 'crb_logo_separator', __('Logo Settings'))
                ->set_classes('separator-class'),
            Field::make('image', 'crb_logo', __('Site Logo'))
                ->set_value_type('url'),
            Field::make('image', 'crb_logo_mobile', __('Mobile Logo'))
                ->set_value_type('url'),
            Field::make('text', 'crb_logo_width', __('Logo Width'))
                ->set_help_text('Enter width in pixels'),
                
            // Header Layout
            Field::make('separator', 'crb_header_layout_separator', __('Header Layout'))
                ->set_classes('separator-class'),
            Field::make('select', 'crb_header_style', __('Header Style'))
                ->add_options(array(
                    'standard' => 'Standard',
                    'centered' => 'Centered',
                    'minimal' => 'Minimal'
                )),
            Field::make('color', 'crb_header_bg_color', __('Header Background Color')),
            Field::make('color', 'crb_header_text_color', __('Header Text Color'))
        ))
        
        ->add_tab(__('Footer Options'), array(
            // Footer Content
            Field::make('separator', 'crb_footer_content_separator', __('Footer Content'))
                ->set_classes('separator-class'),
            Field::make('rich_text', 'crb_footer_text', __('Footer Text')),
            Field::make('complex', 'crb_footer_widgets', __('Footer Widgets'))
                ->add_fields(array(
                    Field::make('text', 'widget_title', __('Widget Title')),
                    Field::make('rich_text', 'widget_content', __('Widget Content'))
                ))
                ->set_layout('tabbed-vertical'),
                
            // Footer Appearance
            Field::make('separator', 'crb_footer_appearance_separator', __('Footer Appearance'))
                ->set_classes('separator-class'),
            Field::make('select', 'crb_footer_columns', __('Footer Columns'))
                ->add_options(array(
                    '1' => 'One Column',
                    '2' => 'Two Columns',
                    '3' => 'Three Columns',
                    '4' => 'Four Columns'
                )),
            Field::make('color', 'crb_footer_bg_color', __('Footer Background Color')),
            Field::make('color', 'crb_footer_text_color', __('Footer Text Color'))
        ));

    // Create User Meta
    Container::make('user_meta', 'User Details')
        ->add_fields(array(
            Field::make('text', 'crb_phone', 'Phone Number'),
            Field::make('text', 'crb_position', 'Job Position')
        ));

    // Create Term Meta
    Container::make('term_meta', 'Category Extra Fields')
        ->where('term_taxonomy', '=', 'category')
        ->add_fields(array(
            Field::make('image', 'crb_cat_image', 'Category Image'),
            Field::make('color', 'crb_cat_color', 'Category Color')
        ));
}

// Example of retrieving the data
function get_post_meta_example() {
    $subtitle = carbon_get_post_meta(get_the_ID(), 'crb_subtitle');
    $summary = carbon_get_post_meta(get_the_ID(), 'crb_summary');
    $gallery = carbon_get_post_meta(get_the_ID(), 'crb_gallery');
    
    // Theme option
    $facebook_url = carbon_get_theme_option('crb_facebook_url');
    
    // User meta
    $user_phone = carbon_get_user_meta(get_current_user_id(), 'crb_phone');
    
    // Term meta
    $category_image = carbon_get_term_meta(get_queried_object_id(), 'crb_cat_image');
}