我正在尝试创建一个新的ACF规则,以便在父页面具有特定模板名称时显示字段。以下是我当前的尝试:
add_filter(\'acf/location/rule_types\', \'acf_location_rules_types\');
function acf_location_rules_types( $choices ) {
$choices[\'Parent\'][\'parent_template\'] = \'Parent Template\';
return $choices;
}
add_filter(\'acf/location/rule_values/parent_template\', \'acf_location_rules_values_parent_template\');
function acf_location_rules_values_parent_template( $choices ) {
$templates = get_page_templates();
if ( $templates ) {
foreach ( $templates as $template_name => $template_filename ) {
$choices[ $template_name ] = $template_name;
}
}
return $choices;
}
add_filter(\'acf/location/rule_match/parent_template\', \'acf_location_rules_match_parent_template\', 10, 3);
function acf_location_rules_match_parent_template( $match, $rule, $options ) {
$selected_template = $rule[\'value\'];
global $post;
$template = get_page_template_slug( $post->post_parent );
if( $rule[\'operator\'] == "==" ) {
$match = ( $selected_template == $template );
} elseif($rule[\'operator\'] == "!=") {
$match = ( $selected_template != $template );
}
return $match;
}
我认为问题在于我试图为当前页面获取父页面模板的方式。我甚至可以在函数内部的挂钩函数中获取父页面模板。php?