Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save campusboy87/b7b1211f8abd4438ae919977f3dcd4a0 to your computer and use it in GitHub Desktop.
Save campusboy87/b7b1211f8abd4438ae919977f3dcd4a0 to your computer and use it in GitHub Desktop.
ACF: Добавление полей к определенной рубрике
<?php
/**
* Статья https://wp-kama.ru/plugin/acf/dobavlenie-polej-k-opredelennoj-rubrike
* Видео https://youtu.be/mHJHnyPLf0M
*/
if ( is_admin() && ! wp_doing_ajax() ) {
add_filter( 'acf/location/rule_types', 'acf_location_rules_types', 999 );
function acf_location_rules_types( $choices ) {
$key = __( 'Forms', 'acf' );
if ( ! isset( $choices[ $key ] ) ) {
$choices[ $key ] = [];
}
$choices[ $key ]['category_id'] = __( 'Category' );
return $choices;
}
add_filter( 'acf/location/rule_values/category_id', 'acf_location_rules_values_category' );
function acf_location_rules_values_category( $choices ) {
$terms = get_terms( 'category', [ 'hide_empty' => false ] );
if ( $terms && is_array( $terms ) ) {
foreach ( $terms as $term ) {
$choices[ $term->term_id ] = $term->name;
}
}
return $choices;
}
add_filter( 'acf/location/rule_match/category_id', 'acf_location_rules_match_category', 10, 3 );
function acf_location_rules_match_category( $match, $rule, $options ) {
$screen = get_current_screen();
if ( $screen && ( $screen->base !== 'term' || $screen->id !== 'edit-category' ) ) {
return $match;
}
$term_id = $_GET['tag_ID'];
$selected_term = $rule['value'];
if ( $rule['operator'] == '==' ) {
$match = ( $selected_term == $term_id );
} elseif ( $rule['operator'] == '!=' ) {
$match = ( $selected_term != $term_id );
}
return $match;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment