admin管理员组文章数量:1435859
I am trying to make a new ACF rule to display fields when parent page has a specific template name. Here's my current attempt:
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;
}
I think the problem is the way I am trying to get parent page template for current page. Can I even get parent page template inside a hook function inside function.php?
I am trying to make a new ACF rule to display fields when parent page has a specific template name. Here's my current attempt:
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;
}
I think the problem is the way I am trying to get parent page template for current page. Can I even get parent page template inside a hook function inside function.php?
Share Improve this question asked Mar 27, 2019 at 18:49 DimChtzDimChtz 1778 bronze badges2 Answers
Reset to default 2For anyone dealing with the same issue, I just needed to change:
$choices[ $template_name ] = $template_name;
with:
$choices[ $template_filename ] = $template_name;
Consider a page template Homepage (page-home.php)
. This way the template name Homepage
will appear on the custom fields page but $rule['value']
will actually return page-home.php
and then we can compare this with get_page_template_slug( $post->post_parent )
.
The problem is not in the way you're getting the parent page nor its template. You can do it exactly like you do.
Problem lies in these two lines:
$choices[ $template_name ] = $template_name;
...
$match = ( $selected_template == $template );
So you're setting template name as choices, but you compare it with filename of the template.
Change the first one to
$choices[ $template_filename ] = $template_name;
And it will work correctly.
本文标签: advanced custom fieldsDisplay ACF if parent page has specific template
版权声明:本文标题:advanced custom fields - Display ACF if parent page has specific template 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745653243a2668554.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论