Support

Account

Home Forums General Issues Custom location rules for an specific Taxonomy Term

Unread

Custom location rules for an specific Taxonomy Term

  • Hello, I’m trying to register a new location to display a list of terms. Yes I need some fields for just 2 specific terms of my custom taxonomy.

    After build the rule and filter it in my functions file I can’t see this one in the locations menu.

    Here is my php Class:

    <?php 
    
    if( ! defined( 'ABSPATH' ) ) exit;
    
    class My_ACF_Location_Taxonomy_Term extends ACF_Location {
    
        public function initialize() {
            $this->name = 'taxonomy_term';
            $this->label = __( "Taxonomy Term", 'acf' );
            $this->category = 'forms';
            $this->object_type = 'term';
        }
    
        public function get_values( $rule ) {
            $choices = array();
        
            // Load all terms, loop over them and append to chcoices.
            $terms = get_terms();
            if( $terms ) {
                foreach( $terms as $term ) {
                    $choices[ $term->term_id ] = $term->name;
                }
            }
            return $choices;
        }
    
        public function match( $rule, $screen, $field_group ) {
    
            // Check screen args for "term_id" which will exist when editing a term.
            // Return false for all other edit screens.
            if( isset($screen['term_id']) ) {
                $term_id = $screen['term_id'];
            } else {
                return false;
            }
    
            // Load the post object for this edit screen.
            $term = get_term( $term_id );
            if( !$term ) {
                return false;
            }
    
            // Compare the Post's author attribute to rule value.
            $result = ( $term->name == $rule['value'] );
    
            // Return result taking into account the operator type.
            if( $rule['operator'] == '!=' ) {
                return !$result;
            }
            return $result;
        }
    }

    And here my function code:

    function my_acf_init_location_types() {
    
    		// Check function exists, then include and register the custom location type class.
    		if( function_exists('acf_register_location_type') ) {
    			include_once( get_template_directory() . 'inc/class-my-acf-location-taxonomy-term.php' );
    			acf_register_location_type( 'My_ACF_Location_Taxonomy_Term' );
    		}
    	}
    
    	add_action('acf/init', 'my_acf_init_location_types');

    Someone could help me with it please.

Viewing 1 post (of 1 total)

The topic ‘Custom location rules for an specific Taxonomy Term’ is closed to new replies.