Site logo

[Support request] generate_element_display with multiple elements

Home Forums Support [Support request] generate_element_display with multiple elements

Home Forums Support generate_element_display with multiple elements

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #2579087
    Web AlchLab

    Hello everyone!

    I’m trying to do a little trick using an element block as a template for parent categories:
    the block will be active on archive pages that have subcategories and once we reached the last branch of the category tree it will be idle and the category will show its article as a “normal” blog page.

    To achieve this i used the filter “generate_element_display” (to bypass the display rules inside the element) and an if statement to determine if category has children or not.
    Everything works fine, except for the fact that in the category where the element is active every other elements are not showing. The only way to show other elements is to add their ID to my function. I thought the “generate_element_display” filter only worked on the element whose id I entered on my function, but seems like the rule will bypass every element that appears on the page.

    Since this is a multilingual site, i would prefer to avoid to manually insert each ID of the elements involved on that category.

    Is there any solution to this? I searched the forum and the documentation but i didn’t find any solution.

    Thanks for your support!

    #2579154
    Web AlchLab

    Guys i’m sorry but i think i had already found the solution. I apologize for not reading the script a 9th time 🙂
    The problem was i included an “else” statement set with “false”.

    I can share my script because it can be useful to someone else:

    function cat_has_children( $term_id = 0, $post_type = ‘post’, $taxonomy = ‘category’ ) {

    $children = get_categories( array(
    ‘child_of’ => $term_id,
    ‘type’ => $post_type,
    ‘taxonomy’ => $taxonomy,
    ‘order’ => ‘ASC’,
    ‘orderby’ => ‘name’ )
    );
    if($children){
    return true;
    }
    }

    add_filter( ‘generate_element_display’, function( $display, $element_id ) {

    $current_cat = get_queried_object();
    $term_id = $current_cat->term_id;

    if ( (is_category() && cat_has_children($term_id, ‘post’, ‘category’)) && (395 === (int) $element_id)) {
    $display = true;
    }

    return $display;

    }, 10, 2 );

    This should be working!

    Bye and sorry for the “false alarm”.

    #2579188
    Ying
    Staff
    Customer Support

    Hi there,

    Glad you’ve found the solution and thanks for sharing it here 🙂

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