如何将Metabox关联到指定页面(以about-us页面为例)
解决方法
要让ACF字段组仅显示在slug为about-us的页面,只需修改字段组配置中的location数组:
- 将
param的值从page_type改为page - 将
value的值从front_page改为目标页面的slug(about-us)或页面ID
修改后的完整代码如下:
<?php acf_add_local_field_group(array ( 'key' => 'group_1', 'title' => 'My Group', 'fields' => array ( array ( 'key' => 'field_1', 'label' => 'Sub Title', 'name' => 'sub_title', 'type' => 'text', 'prefix' => '', 'instructions' => '', 'required' => 0, 'conditional_logic' => 0, 'wrapper' => array ( 'width' => '', 'class' => '', 'id' => '', ), 'default_value' => '', 'placeholder' => '', 'prepend' => '', 'append' => '', 'maxlength' => '', 'readonly' => 0, 'disabled' => 0, ) ), 'location' => array ( array ( array ( 'param' => 'page', 'operator' => '==', 'value' => 'about-us', ), ), ), 'menu_order' => 0, 'position' => 'normal', 'style' => 'default', 'label_placement' => 'top', 'instruction_placement' => 'label', 'hide_on_screen' => '', ));
补充说明
- 如果更倾向于使用页面ID而非slug,直接将
value替换为对应页面的数字ID即可(例如'value' => 123) - 若需要同时匹配多个条件(比如既指定页面类型又指定具体页面),可以在内部数组中添加更多条件规则
内容的提问来源于stack exchange,提问作者Сергей
相关产品推荐
相关产品推荐

