最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin6PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far