-
-
Save billerickson/8e85d4fdf8684c4b90f78c5e28d7b5c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* ACF Rule Match: Page Ancestor | |
* | |
* @author Bill Erickson | |
* @see http://www.billerickson.net/acf-custom-location-rules | |
* | |
* @param boolean $match, whether the rule matches (true/false) | |
* @param array $rule, the current rule you're matching. Includes 'param', 'operator' and 'value' parameters | |
* @param array $options, data about the current edit screen (post_id, page_template...) | |
* @return boolean $match | |
*/ | |
function ea_acf_rule_match_page_ancestor( $match, $rule, $options ) { | |
if ( ! $options['post_id'] || 'page' !== get_post_type( $options['post_id'] ) ) | |
return false; | |
$ancestors = get_ancestors( $options['post_id'], 'page' ); | |
$is_ancestor = in_array( $rule['value'], $ancestors ); | |
if ( '==' == $rule['operator'] ) { | |
$match = $is_ancestor; | |
} elseif ( '!=' == $rule['operator'] ) { | |
$match = ! $is_ancestor; | |
} | |
return $match; | |
} | |
add_filter( 'acf/location/rule_match/page_ancestor', 'ea_acf_rule_match_page_ancestor', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment