advanced custom fields - Display ACF if parent page has specific template

I am trying to make a new ACF rule to display fields when parent page has a specific template name. Here's my curre

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 badges
Add a comment  | 

2 Answers 2

Reset to default 2

For 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745653243a4638389.html

相关推荐

  • advanced custom fields - Display ACF if parent page has specific template

    I am trying to make a new ACF rule to display fields when parent page has a specific template name. Here's my curre

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信