[Support request] Custom condition to Layout Elements

Home Forums Support [Support request] Custom condition to Layout Elements

Home Forums Support Custom condition to Layout Elements

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #2273299
    Lox

    Hello,

    Basically, I need a page header for first level pages (does not have a parent), and another for children pages (has parent). But I see other use cases.

    Is it possible to implement it with hooks ?

    Regards.

    #2273305
    Fernando
    Customer Support

    Hi Lox,

    Yes, it’s possible.

    A specific filter for Hook Elements you may use is generate_hook_element_display.

    Reference: https://docs.generatepress.com/article/generate_hook_element_display/

    So for instance, here’s a PHP snippet you may try:

    add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
        if ( 396602 === $element_id && is_page() ) {
    		if( ! empty ( get_ancestors ( get_the_ID(), 'page' ) ) ) {
    			//display if child
    			$display = true;
    		} else {
    			$display = false;
    		}
        } else if ( 706 === $element_id  && is_page() ) {
    		if( empty ( get_ancestors ( get_the_ID(), 'page' ) ) ) {
    			//display if parent
    			$display = true;
    		} else {
    			$display = false;
    		}
    	}
    
        return $display;
    }, 10, 2 );

    Kindly replace 396602 with the Hook Element ID meant for the child, and 706 for the parent pages.

    This only works for pages.

    You would need to alter it for posts.

    If you’re planning to use a Block Element instead for instance, just replace generate_hook_element_display with generate_block_element_display.

    Hope this clarifies!

    #2273473
    Lox

    Hard coding element ID is not really recommended …

    #2273614
    Lox

    Could you please look for a way to programmatically add custom conditions in the metabox select ? I would be then up to the developer to use a hook to add its own conditions and another to check for them.

    #2273662
    David
    Staff
    Customer Support

    Hi there,

    its not possible at this time, we are reviewing the display conditions functions for a future update.
    For now – you would need to use the method that Fernando provided.

    #2275370
    Lox

    Hi,

    I had a look. Adding a filter on the output of the get_conditions() (GeneratePress_Conditions class) and another filtering the output of show_data() method would do it.

    Regards.

    #2275853
    David
    Staff
    Customer Support

    Thanks for sharing that.
    As i said we are reviewing the display conditions, we’re most likely to rebuild the current system so more complex conditions can be built within the UI. And with that we would include additional filters.

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