如何禁用页面模板中的显示自定义字段?

时间:2019-09-20 12:09:39

标签: wordpress advanced-custom-fields

我通过代码创建了自定义字段,然后使用以下代码进行注册:

/**
 * Content blocks for all pages
 */
$location_page = new ACF_Group_Location;
$location_page->add_rule( 'post_type', 'page');

$location_slots = new ACF_Group_Location;
$location_slots->add_rule( 'post_type', 'slots' );

$location_card = new ACF_Group_Location;
$location_card->add_rule( 'post_type', 'card' );



ACF_Group::create( 'content-blocks', __( 'Content blocks', 'dw' ) )
    ->add_location( $location_page )
    ->add_location( $location_slots )
    ->add_location( $location_card )
    ->set_attr( 'label_placement', 'top' )
    ->set_attr( 'hide_on_screen', array( 'the_content', 'comments' ) )
    ->add_fields( array(
        array(
            'label' => __( 'Content blocks', 'dw' ),
            'name' => 'cb',
            'type' => 'flexible_content',
            'button_label' => __( 'Add content block', 'dw' ),
            'layouts' => $cb
        ),
    )
)->register();

add_rule()包括:

public function add_rule( $param, $value, $operator = '==' ) {
        $this->rules[] = compact( 'param', 'value', 'operator' );

        return $this;
    }

我的问题是,如果要在页面模板上禁用它们,我需要添加什么代码?

1 个答案:

答案 0 :(得分:0)

因此,您通过this

以编程方式使用ACF

与其将自定义字段添加到所有页面,然后尝试对特定模板禁用它,只需将自定义字段组添加到所需模板即可。

<?php
add_action( 'register_acf_groups', 'my_theme_register_acf_groups' );
function mytheme_register_acf_groups() {
    ACF_Group::create( 'my_group', 'My Group' )
        ->add_location_rule( 'post_type', 'page' )
        ->add_location_rule( 'page_template', 'template-contact.php' ) // <- see here
        ->set_attr( 'label_placement', 'left' )
        ->add_fields(array(
            array(
                'label' => 'Test Field',
                'name'  => 'test_field',
                'type'  => 'text'
            ),
        ))
        ->register();
}