Created
January 16, 2016 01:47
-
-
Save nocean/1c13c545d501f5da07b7 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
/** | |
* Add a custom location rule for Comment Type | |
*/ | |
add_filter('acf/location/rule_types', 'THEMENAME_acf_location_rules_types'); | |
function THEMENAME_acf_location_rules_types( $choices ) { | |
// This creates a new group in the Location dropdown called "Comments" | |
// And adds a child-item called "Comments on Post Type" | |
$choices['Comments']['comments_on_post_type'] = 'Comments on Post Type'; | |
return $choices; | |
} | |
/** | |
* Populate the custom location rule's choices | |
*/ | |
add_filter('acf/location/rule_values/comments_on_post_type', 'THEMENAME_acf_location_rules_values_user'); | |
function THEMENAME_acf_location_rules_values_user( $choices ) { | |
// Get all the "public" post types. | |
$post_types = get_post_types(array('public' => true)); | |
// Assuming there are some, loop through and add to the $choices array. | |
if( $post_types ) { | |
foreach( $post_types as $post_type ) { | |
$choices[ $post_type ] = $post_type; | |
} | |
} | |
return $choices; | |
} | |
/* | |
* Perform the match | |
*/ | |
add_filter('acf/location/rule_match/comments_on_post_type', 'THEMENAME_acf_location_rules_match_comment_post_type', 10, 3); | |
function THEMENAME_acf_location_rules_match_comment_post_type( $match, $rule, $options ) { | |
global $post; | |
// Get the post type of the current page, as well as the one we've selected | |
$current_post_type = $post->post_type; | |
$selected_post_type = $rule['value']; | |
// Depending on your operator, compare and return a boolean for ACF to use. | |
if ($rule['operator'] == "==") { | |
$match = ( $current_post_type == $selected_post_type ); | |
} elseif ($rule['operator'] == "!=") { | |
$match = ( $current_post_type != $selected_post_type ); | |
} | |
return $match; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment