假设您创建一个WordPress帖子,并为其分配一个值为bar
的元密钥foo
。仅当foo
等于bar
时,才需要显示ACF字段。然而,ACF中并没有内置的位置规则来实现这一点。您将如何通过创建ACF自定义位置规则来解决此问题?
如果post元键等于或不等于某个值,则要显示ACF字段,请使用以下代码段。它基于ACF自定义位置规则指南。
if( ! defined( 'ABSPATH' ) ) exit;
class My_ACF_Location_Post_Meta extends ACF_Location {
public function initialize() {
$this->name = 'post_meta';
$this->label = __( "Post Meta", 'acf' );
$this->category = 'post';
$this->object_type = 'post';
}
public function rule_values($choices, $rule){
if(!acf_is_screen('acf-field-group') && !acf_is_ajax('acf/field_group/render_location_rule')){
return array(
$rule['meta_key'] => $rule['meta_key'],
$rule['meta_value'] => $rule['meta_value']
);
}
ob_start();
acf_render_field(array(
'type' => 'text',
'name' => 'meta_key',
'prefix' => 'acf_field_group[location]['.$rule['group'].']['.$rule['id'].']',
'value' => (isset($rule['meta_key']) ? $rule['meta_key'] : ''),
'placeholder' => 'Meta Key'
));
acf_render_field(array(
'type' => 'text',
'name' => 'meta_value',
'prefix' => 'acf_field_group[location]['.$rule['group'].']['.$rule['id'].']',
'value' => (isset($rule['meta_value']) ? $rule['meta_value'] : ''),
'placeholder' => 'Meta Value'
));
return ob_get_clean();
}
public function rule_operators($choices, $rule){
$choices = [];
$choices['key_is_equal_to_value'] = __('key is equal to value', 'acf');
$choices['key_is_not_equal_to_value'] = __('key is not equal to value', 'acf');
return $choices;
}
public function match( $rule, $screen, $field_group ) {
// Check screen args for "post_id" which will exist when editing a post.
// Return false for all other edit screens.
if( isset($screen['post_id']) ) {
$post_id = $screen['post_id'];
} elseif (isset($screen['attachment_id'])) {
$post_id = $screen['attachment_id'];
} else {
return false;
}
// Load the post meta object for this edit screen.
$post_meta_value = get_post_meta( $post_id, $rule['meta_key'], true );
if( !$post_meta_value ) {
return false;
}
// Compare the Post's meta value to rule meta value.
$result = ( strval($post_meta_value) == $rule['meta_value'] );
// Return result taking into account the operator type.
if( $rule['operator'] == 'key_is_not_equal_to_value' ) {
return !$result;
}
return $result;
}
}
add_action('acf/init', 'my_acf_init_location_types');
function my_acf_init_location_types() {
// Check function exists, then include and register the custom location type class.
if( function_exists('acf_register_location_type') ) {
acf_register_location_type( 'My_ACF_Location_Post_Meta' );
}
}