-
-
Save billerickson/bc2f167833f071397fa3437e13dfd584 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 Type | |
* | |
* @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_type( $match, $rule, $options ) { | |
// Only run for 'no_children' value | |
if ( 'no_children' != $rule['value'] ) { | |
return $match; | |
} | |
// Only run if post ID is defined and this is a page | |
if ( ! $options['post_id'] || 'page' != get_post_type( $options['post_id'] ) ) { | |
return $match; | |
} | |
$children = get_pages( array( 'child_of' => get_the_ID() ) ); | |
if ( '==' == $rule['operator'] ) { | |
$match = empty( $children ); | |
} elseif ( '!=' == $rule['operator'] ) { | |
$match = ! empty( $children ); | |
} | |
return $match; | |
} | |
add_filter( 'acf/location/rule_match/page_type', 'ea_acf_rule_match_page_type', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment