Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

GP Elements for Child Terms

  • Hi, I have a Taxonomy called “Tour Themes”, inside that i have a Term called ‘Pilgrimage Tours’, I have a layout for it..

    But that term has multiple child terms like ‘Abc Pilgrimage Tour’, ‘XYZ Pilgrimage Tour’ so i want a different Element for these child terms of ‘Pilgrimage Tours’ only..

    How this is possible?

    Theme Taxonomy:
    Term: Pilgrimage Tours –> I have an Element for this.
    Child Terms: — ABC Pilgrimage Tour, — XYZ Pilgrimage Tour –> I want an element only for the children terms of ‘Pilgrimage Tours’

    Thanks.

  • Hi there,

    If you do not have many child terms, you can select them one by one in the element’s location.

    If you want to do it automatically, you will need to use PHP code. Something like this:
    https://docs.generatepress.com/article/generate_element_display/#display-an-element-to-all-single-posts-that-are-in-the-subcatego

  • Can you please give me the exact whole code?

    And make sure it’s not pages, it’s Taxonomy term.

  • what is the element type? content template?

    If so, do you want it to show on the child terms’ archives or single posts/cpts that have the child terms?

  • It’s loop template obviously, as i mentioned terms..

    Child terms archive..

  • Hello.

    Try this:

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        // Replace 123 with your actual Element ID
        if ( 123 !== $element_id ) {
            return $display;
        }
    
        // Check if we're on a taxonomy term archive
        if ( ! is_tax( 'tour-theme' ) ) {
            return false;
        }
    
        // Get the current term
        $current_term = get_queried_object();
        
        // Replace 'pilgrimage-tours' with your parent term slug
        $parent_term_slug = 'pilgrimage-tours';
        
        // Get the parent term object
        $parent_term = get_term_by( 'slug', $parent_term_slug, 'tour-theme' );
        
        if ( ! $parent_term ) {
            return false;
        }
        
        // Check if current term IS the parent term
        if ( $current_term->term_id === $parent_term->term_id ) {
            return $display; // Display on parent term
        }
        
        // Check if current term is a child of the specified parent
        if ( $current_term->parent === $parent_term->term_id ) {
            return $display; // Display on child terms
        }
        
        return false; // Don't display for other terms
    }, 10, 2 );

    1. Set your loop template to display for the Entire Site and replace 123 with the actual Element ID (you can find this in the Element URL, eg: wp-admin/post.php?post=123&action=edit)

    2. Replace tour-theme with your actual taxonomy slug (if different)

    3. Replace pilgrimage-tours with the slug of your parent term (if different)

    Let us know how it goes!

  • Working but the issue is, Tour Themes has a separate template also… So i have to exclude that ‘Tour Themes’ loop template for these child ones…

    Also it is showing on the parent as well..

    Thanks

  • Hi there,

    Try this snippet:

    add_filter( 'generate_element_display', function( $display, $element_id ) {
    
          // Your Element IDs
          $tour_themes_template_id   = 456; // General Tour Themes template
          $child_terms_element_id    = 789; // Element for Pilgrimage Tours children
    
          // Only process our target elements
          if ( ! in_array( $element_id, [ $tour_themes_template_id, $child_terms_element_id ], true ) ) {
              return $display;
          }
    
          if ( ! is_tax( 'tour-theme' ) ) {
              return $element_id === $child_terms_element_id ? false : $display;
          }
    
          $current_term = get_queried_object();
          $parent_term = get_term_by( 'slug', 'pilgrimage-tours', 'tour-theme' );
    
          if ( ! $current_term || ! $parent_term ) {
              return $display;
          }
    
          $is_pilgrimage_child = ( $current_term->parent === $parent_term->term_id );
    
          // Tour Themes template: HIDE on Pilgrimage children
          if ( $element_id === $tour_themes_template_id && $is_pilgrimage_child ) {
              return false;
          }
    
          // Child terms element: SHOW only on Pilgrimage children
          if ( $element_id === $child_terms_element_id ) {
              return $is_pilgrimage_child ? true : false;
          }
    
          return $display;
    
      }, 10, 2 );

    Make sure to replace the taxonomy and term slugs with the actual values.

  • Didn’t work the way I wanted…

    Okay, let’s make it simple…

    Can I just hide a section, on Tour Themes template if it’s Child terms archive of Pilgrimage Tours..

    It is just a simple filter on sidebar i wanna hide…

    Thanks…

  • Do you have GB pro? If so, you can create a location condition for the section you want to remove.

    on Tour Themes template if it’s Child terms archive of Pilgrimage Tours..

    However, I don’t quite understand the logic here, can you link us to the page in question and let us know which part you want to hide on it?

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.