Add custom display condition to Elementor Theme Builder for custom taxonomy children, grandchildren, and great-grandchildren

admin2025-01-07  5

I have a few templates built with Elementor Theme Builder. However, the option to display them on nested archives is quite limited. Here's my case

I have a custom taxonomy (tags) called Locations. Which is then further nested as listings are added. It gets the address and breaks it down to Region, City, and Locality, and inserts then set the terms. No dramas here. This is how it looks.

AND

I have the following templates built into the Theme builder.

  1. Region Template (Applies to Waikato, also all other regions)
  2. City Template (Applies to Hamilton, Also all other other cities)
  3. Locality Template (Applies to Frankton 3204, all to all other localities)
  4. Service Template (Applies to Coffee shop in Frankton, also to all other services)

Elementor only allows applying templates to

  1. All locations (this includes the children's terms)
  2. Direct children of Locations
  3. Any child of Location

However in my case, I want to apply 4 different templates to the parent (region), child (city), grandchild (locality), and great-grandchild (service in the locality).

Elementor allows one to register custom conditions in Theme builder, however, I am not able to. Welp!

add_action( 'elementor/theme/register_conditions', function( $conditions_manager ) { 
class Subcategory_Archive extends ElementorPro\Modules\ThemeBuilder\Conditions\Taxonomy {
    private $taxonomy;

    public function get_name() {
        return 'child_of_' . $this->taxonomy->name;
    }

    public function get_label() {
        return sprintf( __( 'Direct Child %s Of', 'elementor-pro' ), $this->taxonomy->labels->singular_name );
    }

    public function __construct( $data ) {
        parent::__construct( $data );

        $this->taxonomy = $data['object'];
    }

    public function is_term() {
        $taxonomy = $this->taxonomy->name;
        $current = get_queried_object();
        return ( $current && isset( $current->taxonomy ) && $taxonomy === $current->taxonomy );
    }

    public function check( $args ) {
        $id = (int) $args['id'];
        /**
         * @var \WP_Term $current
         */
        $current = get_queried_object();
        if ( ! $this->is_term() || 0 === $current->parent ) {
            return false;
        }

        while ( $current->parent > 0 ) {
            if ( $id === $current->parent ) {
                return true;
            }
            $current = get_term_by( 'id', $current->parent, $current->taxonomy );
        }

        return $id === $current->parent;
    }
}
$taxonomy = get_term('location');

$conditions_manager->get_condition( 'listing' )->register_sub_condition( new Subcategory_Archive([ 'object' => $taxonomy ]) );
}, 100 );

I need help to register custom conditions for the archive pages to display those taxononmies.

NOTE: there's option of individually selecting each term and applying the template. However, I have over 2500 listing categories (i.e Coffee shop) and thousands of localities and cities. I can manage regions because there are only 16 regions in NZ.

Elementor Docs: /

I have a few templates built with Elementor Theme Builder. However, the option to display them on nested archives is quite limited. Here's my case

I have a custom taxonomy (tags) called Locations. Which is then further nested as listings are added. It gets the address and breaks it down to Region, City, and Locality, and inserts then set the terms. No dramas here. This is how it looks.

AND

I have the following templates built into the Theme builder.

  1. Region Template (Applies to Waikato, also all other regions)
  2. City Template (Applies to Hamilton, Also all other other cities)
  3. Locality Template (Applies to Frankton 3204, all to all other localities)
  4. Service Template (Applies to Coffee shop in Frankton, also to all other services)

Elementor only allows applying templates to

  1. All locations (this includes the children's terms)
  2. Direct children of Locations
  3. Any child of Location

However in my case, I want to apply 4 different templates to the parent (region), child (city), grandchild (locality), and great-grandchild (service in the locality).

Elementor allows one to register custom conditions in Theme builder, however, I am not able to. Welp!

add_action( 'elementor/theme/register_conditions', function( $conditions_manager ) { 
class Subcategory_Archive extends ElementorPro\Modules\ThemeBuilder\Conditions\Taxonomy {
    private $taxonomy;

    public function get_name() {
        return 'child_of_' . $this->taxonomy->name;
    }

    public function get_label() {
        return sprintf( __( 'Direct Child %s Of', 'elementor-pro' ), $this->taxonomy->labels->singular_name );
    }

    public function __construct( $data ) {
        parent::__construct( $data );

        $this->taxonomy = $data['object'];
    }

    public function is_term() {
        $taxonomy = $this->taxonomy->name;
        $current = get_queried_object();
        return ( $current && isset( $current->taxonomy ) && $taxonomy === $current->taxonomy );
    }

    public function check( $args ) {
        $id = (int) $args['id'];
        /**
         * @var \WP_Term $current
         */
        $current = get_queried_object();
        if ( ! $this->is_term() || 0 === $current->parent ) {
            return false;
        }

        while ( $current->parent > 0 ) {
            if ( $id === $current->parent ) {
                return true;
            }
            $current = get_term_by( 'id', $current->parent, $current->taxonomy );
        }

        return $id === $current->parent;
    }
}
$taxonomy = get_term('location');

$conditions_manager->get_condition( 'listing' )->register_sub_condition( new Subcategory_Archive([ 'object' => $taxonomy ]) );
}, 100 );

I need help to register custom conditions for the archive pages to display those taxononmies.

NOTE: there's option of individually selecting each term and applying the template. However, I have over 2500 listing categories (i.e Coffee shop) and thousands of localities and cities. I can manage regions because there are only 16 regions in NZ.

Elementor Docs: https://developers.elementor.com/docs/theme-conditions/

Share Improve this question edited Dec 3, 2022 at 12:57 mardag asked Dec 3, 2022 at 12:52 mardagmardag 13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

did you get any further with that issue? I'm facing same right now

Update

Everyone having trouble solving this, have a look @ elementors github issue. The magic lays in the check function

https://github.com/elementor/elementor/issues/8155

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736256001a326.html

最新回复(0)