【问题标题】:WP ACF Generate php – Where and how to put the code?WP ACF Generate php – 在哪里以及如何放置代码?
【发布时间】:2019-07-23 12:15:58
【问题描述】:

在我的 WordPress 网站上,我安装了 ACF 插件,并以“常规”方式使用多个页面上的字段。所以我在后端创建了组和字段。一切正常。

现在我想生成其中几个字段组的 php 代码(因为我想稍微更改代码)。我将代码放在哪里?最好的方法是什么?

我创建了一个新的 php 文件 acf-deals.php 并将其上传到根目录下的 /php-includes/ 目录。它包括以下代码:

<?php

if( function_exists('acf_add_local_field_group') ):

acf_add_local_field_group(array(
    'key' => 'group_deal1',
    'title' => 'Deal One (displayed on homepage)',
    'fields' => array(
        array(
            'key' => 'field_5c66e017f8359',
            'label' => 'Title',
            'name' => 'deal_title_one',
            'type' => 'text',
            'instructions' => '(max. 100 characters)',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => 'deals-title',
                'id' => '',
            ),
            'default_value' => '',
            'placeholder' => '',
            'prepend' => '',
            'append' => '',
            'maxlength' => 100,
        ),
        array(
            'key' => 'field_5c6d43781bcb1',
            'label' => 'Deal or Discount',
            'name' => 'discount_deal_or_special_one',
            'type' => 'radio',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => array(
                array(
                    array(
                        'field' => 'field_5c66e017f8359',
                        'operator' => '!=empty',
                    ),
                ),
            ),
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'choices' => array(
                'Discount' => 'Discount',
                'Deal' => 'Deal',
            ),
            'allow_null' => 1,
            'other_choice' => 0,
            'default_value' => '',
            'layout' => 'vertical',
            'return_format' => 'value',
            'save_other_choice' => 0,
        ),
        array(
            'key' => 'field_5c66e094f835a',
            'label' => 'Description',
            'name' => 'deal_description_one',
            'type' => 'textarea',
            'instructions' => '(max. 600 characters)',
            'required' => 0,
            'conditional_logic' => array(
                array(
                    array(
                        'field' => 'field_5c66e017f8359',
                        'operator' => '==empty',
                    ),
                ),
            ),
            'wrapper' => array(
                'width' => '',
                'class' => 'deals-description',
                'id' => '',
            ),
            'default_value' => '',
            'placeholder' => '',
            'maxlength' => 600,
            'rows' => '',
            'new_lines' => '',
        ),
        array(
            'key' => 'field_5c66e0d4f835b',
            'label' => 'Regular price',
            'name' => 'regular_price_one',
            'type' => 'number',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => 'deals-regular-price',
                'id' => '',
            ),
            'default_value' => '',
            'placeholder' => '',
            'prepend' => '$',
            'append' => '',
            'min' => '',
            'max' => '',
            'step' => '',
        ),
        array(
            'key' => 'field_5c66e130f835c',
            'label' => 'Discounted price / deal price',
            'name' => 'discounted_price_one',
            'type' => 'number',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => 'deals-discounted-price',
                'id' => '',
            ),
            'default_value' => '',
            'placeholder' => '',
            'prepend' => '$',
            'append' => '',
            'min' => '',
            'max' => '',
            'step' => '',
        ),
        array(
            'key' => 'field_5c703c1fd5851',
            'label' => 'Valid until',
            'name' => 'valid_until_one',
            'type' => 'date_picker',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'display_format' => 'm/d/Y',
            'return_format' => 'm/d/Y',
            'first_day' => 1,
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'post',
            ),
            array(
                'param' => 'post_format',
                'operator' => '==',
                'value' => 'aside',
            ),
        ),
        array(
            array(
                'param' => 'post_format',
                'operator' => '==',
                'value' => 'status',
            ),
        ),
    ),
    'menu_order' => 1,
    'position' => 'acf_after_title',
    'style' => 'default',
    'label_placement' => 'top',
    'instruction_placement' => 'label',
    'hide_on_screen' => '',
    'active' => 1,
    'description' => '',
));

endif;

?>

我已将此代码添加到我的 single-aside.php 文件中:

<?php get_template_part( '/php-includes/acf-deals.php', 'acf-deals' ); ?>

不幸的是,该字段未显示在 WP 管理后端。我究竟做错了什么?我搜索和搜索,但找不到正确的解释或修复它的方法。我希望有人可以提供帮助。

感谢您的帮助!

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:
    add_action('acf/init', function(){ 
        ... add local fields
    })
    

    【讨论】:

    • 欢迎来到 Stackoverflow。您介意为其他程序员扩展您的答案吗?谁可能了解它如何帮助解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多