我通过代码创建了自定义字段,然后使用以下代码进行注册:
/**
* 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;
}
我的问题是,如果要在页面模板上禁用它们,我需要添加什么代码?
答案 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();
}