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

本文标签: advanced custom fieldsDisplay ACF if parent page has specific template