在 functions.php (WordPress) 中包含 ACF

Posted

技术标签:

【中文标题】在 functions.php (WordPress) 中包含 ACF【英文标题】:Including ACF in functions.php (WordPress) 【发布时间】:2013-10-24 16:49:10 【问题描述】:

我正在制作第一次在主题 forrest 上出售的主题。我在开发过程中使用 ACF 作为标准插件,以确保一切正常。

我几乎完成了,所以决定将我的ACF 导出到php 并将其包含到我的functions.php 文件中。

我将它给我的代码复制/粘贴到functions.php 中,并将ACFoptions-pagerepeater-field 包含在我的主题内的文件夹中,并相应地引用它们。

但是,当我访问我的网站时,我收到了这个错误:

Fatal error: Cannot redeclare class Acf in /Applications/MAMP/htdocs/omg/wp-content/themes/omg-v1/external/acf/acf.php on line 46

我也从插件文件夹中删除了实际的插件文件。

任何想法为什么它可能会被窃听?

我知道代码很多,但如果有人想看看这是我 functions.php 中的代码(ACF 给了我)

include_once('external/acf/acf.php' );
define( 'ACF_LITE' , true );

// Add-ons 
include_once('external/acf-repeater/acf-repeater.php');
include_once('external/acf-options-page/acf-options-page.php');



if(function_exists("register_field_group"))

    register_field_group(array (
        'id' => 'acf_featured-story',
        'title' => 'Featured Story',
        'fields' => array (
            array (
                'key' => 'field_5256bd0fc7592',
                'label' => 'Is this story featured?',
                'name' => 'featured-story',
                'type' => 'true_false',
                'message' => 'Yes',
                'default_value' => 0,
            ),
        ),
        'location' => array (
            array (
                array (
                    'param' => 'post_type',
                    'operator' => '==',
                    'value' => 'post',
                    'order_no' => 0,
                    'group_no' => 0,
                ),
            ),
        ),
        'options' => array (
            'position' => 'side',
            'layout' => 'default',
            'hide_on_screen' => array (
            ),
        ),
        'menu_order' => 0,
    ));
    register_field_group(array (
        'id' => 'acf_options',
        'title' => 'Options',
        'fields' => array (
            array (
                'key' => 'field_525d1b6d49043',
                'label' => 'Homepage Layout (Left Column)',
                'name' => 'homepage-layout-left',
                'type' => 'repeater',
                'instructions' => 'Select which categories you want to display on the homepage.',
                'sub_fields' => array (
                    array (
                        'key' => 'field_525d1b8a49044',
                        'label' => 'Category',
                        'name' => 'category',
                        'type' => 'taxonomy',
                        'column_width' => '',
                        'taxonomy' => 'category',
                        'field_type' => 'select',
                        'allow_null' => 0,
                        'load_save_terms' => 0,
                        'return_format' => 'id',
                        'multiple' => 0,
                    ),
                    array (
                        'key' => 'field_525d2473de72c',
                        'label' => 'Number of Posts',
                        'name' => 'number-of-posts',
                        'type' => 'number',
                        'column_width' => '',
                        'default_value' => 4,
                        'placeholder' => '',
                        'prepend' => '',
                        'append' => '',
                        'min' => 2,
                        'max' => '',
                        'step' => 2,
                    ),
                ),
                'row_min' => 1,
                'row_limit' => 4,
                'layout' => 'row',
                'button_label' => 'Add a category',
            ),
            array (
                'key' => 'field_525d25ee91b41',
                'label' => 'Homepage Layout (Sidebar)',
                'name' => 'homepage-layout-sidebar',
                'type' => 'repeater',
                'instructions' => 'Select which categories you want to display in the sidebar.',
                'sub_fields' => array (
                    array (
                        'key' => 'field_525d25ee91b42',
                        'label' => 'Category',
                        'name' => 'category',
                        'type' => 'taxonomy',
                        'column_width' => '',
                        'taxonomy' => 'category',
                        'field_type' => 'select',
                        'allow_null' => 0,
                        'load_save_terms' => 0,
                        'return_format' => 'id',
                        'multiple' => 0,
                    ),
                    array (
                        'key' => 'field_525d25ee91b43',
                        'label' => 'Number of Posts',
                        'name' => 'number-of-posts',
                        'type' => 'number',
                        'column_width' => '',
                        'default_value' => 4,
                        'placeholder' => '',
                        'prepend' => '',
                        'append' => '',
                        'min' => 2,
                        'max' => '',
                        'step' => 2,
                    ),
                ),
                'row_min' => 1,
                'row_limit' => 4,
                'layout' => 'row',
                'button_label' => 'Add a category',
            ),
            array (
                'key' => 'field_525d2afdf101b',
                'label' => 'Newsletter (optional)',
                'name' => 'newsletter',
                'type' => 'repeater',
                'sub_fields' => array (
                    array (
                        'key' => 'field_525d2b6612426',
                        'label' => 'Link',
                        'name' => 'link',
                        'type' => 'text',
                        'instructions' => 'If you have a newsletter, copy/paste your link in here.',
                        'column_width' => '',
                        'default_value' => '',
                        'placeholder' => '',
                        'prepend' => '',
                        'append' => '',
                        'formatting' => 'none',
                        'maxlength' => '',
                    ),
                    array (
                        'key' => 'field_525d2bb40e332',
                        'label' => 'Text',
                        'name' => 'text',
                        'type' => 'textarea',
                        'instructions' => 'Enter your call to action for the newsletter here.',
                        'column_width' => '',
                        'default_value' => 'Subscribe!

                        To the OMG! newsletter!',
                        'placeholder' => '',
                        'maxlength' => 100,
                        'formatting' => 'br',
                    ),
                ),
                'row_min' => 0,
                'row_limit' => 1,
                'layout' => 'row',
                'button_label' => 'Add Newsletter Link',
            ),
            array (
                'key' => 'field_525e8b77edf6e',
                'label' => 'Advert',
                'name' => 'adverts',
                'type' => 'repeater',
                'instructions' => 'Do you wish to display an advert in your sidebar?',
                'sub_fields' => array (
                    array (
                        'key' => 'field_525e8c1e3600f',
                        'label' => 'Advert Image',
                        'name' => 'advert-img',
                        'type' => 'image',
                        'instructions' => 'Upload a 300 x 200px image for best quality / load time',
                        'column_width' => '',
                        'save_format' => 'url',
                        'preview_size' => 'full',
                        'library' => 'all',
                    ),
                    array (
                        'key' => 'field_525e8d1699619',
                        'label' => 'Link',
                        'name' => 'ad-link',
                        'type' => 'text',
                        'column_width' => '',
                        'default_value' => '',
                        'placeholder' => '',
                        'prepend' => '',
                        'append' => '',
                        'formatting' => 'none',
                        'maxlength' => '',
                    ),
                ),
                'row_min' => 0,
                'row_limit' => 1,
                'layout' => 'table',
                'button_label' => 'Add an Advert',
            ),
        ),
        'location' => array (
            array (
                array (
                    'param' => 'options_page',
                    'operator' => '==',
                    'value' => 'acf-options',
                    'order_no' => 0,
                    'group_no' => 0,
                ),
            ),
        ),
        'options' => array (
            'position' => 'normal',
            'layout' => 'no_box',
            'hide_on_screen' => array (
            ),
        ),
        'menu_order' => 0,
    ));

【问题讨论】:

【参考方案1】:

在functions.php中包含ACF只需四个步骤

    /***********************************************************
    *               Integrate ACF in theme
    ***********************************************************/
    // 1. customize ACF path
    add_filter('acf/settings/path', 'my_acf_settings_path'); 
    function my_acf_settings_path( $path )  
        // update path
        $path = get_stylesheet_directory() . '/framework/acf/';    
        // return
        return $path;    
     

    // 2. customize ACF dir
    add_filter('acf/settings/dir', 'my_acf_settings_dir'); 
    function my_acf_settings_dir( $dir )  
        // update path
        $dir = get_stylesheet_directory_uri() . '/framework/acf/';    
        // return
        return $dir;    
     

    // 3. Hide ACF field group menu item
    add_filter('acf/settings/show_admin', '__return_false');

    // 4. Include ACF
    include_once( get_stylesheet_directory() . '/framework/acf/acf.php' );

    // including acf fields
    require_once( get_template_directory() . '/framework/acf-fields.php' );

【讨论】:

【参考方案2】:

您需要确保您的包含不会与 ACF 本身或也使用它的其他人的插件发生冲突。

if( !class_exists('Acf') )
    include_once('external/acf/acf.php' );

if( !class_exists('acf_repeater_plugin') )
    include_once('external/acf-repeater/acf-repeater.php');

if( !class_exists('acf_options_page_plugin') )
    include_once('external/acf-options-page/acf-options-page.php');

【讨论】:

以上是关于在 functions.php (WordPress) 中包含 ACF的主要内容,如果未能解决你的问题,请参考以下文章

在 functions.php (WordPress) 中包含 ACF

text 在Wordpres中添加自定义Jquery脚本

PHP 在functions.php中启用hovercards

在functions.php中获取wordpress帖子ID

php 在functions.php中声明后显示小部件

php Cheatsheet - 在functions.php中的WooCommerce定制