SOLVED: Conditional Display Logic / Intended Behavior

I am using the new Conditional Display filter in 1.5 Beta and have a question about the intended behavior. I have a section setup within the header that displays if a page header image is present in an ACF image field (assigned to each page). If none assigned, then hide the entire section. The logic works correctly when editing the header (I can change previews of different pages and see the section showing and hiding according to the logic. It also works on the front-end. But when editing a page, it does not seem to be respected (nothing displays for any page). Just wondering if this was the intended behavior - or if this is a bug that should be reported. Below are screenshots:

Editing Header (Merchandise Page preview applied):
Page Header image and title is present (correct result)

Editing Merchandise Page:
Page header and title is not present (incorrect result)

Viewing on Front-end:
Page Header image and title is present (correct result)

The code I am using is the following:

// Render an element with "section-page-header" HTML ID if the specified condition is true.

add_filter( 'bricks/element/render', function( $render, $element ) {	
	// Get the element HTML ID
	$html_id = ! empty( $element->attributes['_root']['id'] ) ? $element->attributes['_root']['id'] : false;

	if ( 'section-page-header' === $html_id ) {
		return get_post_meta( get_the_ID(), 'fld_page_header_image', true );
	}

	return $render;
}, 10, 2 );
1 Like

Hi,

While we wait for @timmse to provide the correct answer to your question, just want to add:

Have you considered moving that page header section to inside the Page Template instead of in the Header?

@Sridhar - Yep - my thoughts exactly. However, I tried this and it does not work either. I am thinking maybe it is not picking up the global id variable of the page when this is called (within the Builder - as front-end works fine):

return get_post_meta( get_the_ID(), 'fld_page_header_image', true );

@bmc38119 Yep, that seems to be an issue related to the post ID.

Can you try replacing get_the_ID() in your code with $element->post_id and let me know if this fixes the issue?

@thomas Just tested it and this does not correct the issue. The template displays correctly when editing the page template and previewing the page in the template, but not when editing the actual page that is using the template. It does show correctly on front-end though.

Possibly related: Single Page template design isn't showing when editing a page

Hello @bmc38119

I’ve tested the same scenario locally and it is working for me.

The code I’m using:

add_filter( 'bricks/element/render', function( $render, $element ) {	
	// Get the element HTML ID
	$html_id = ! empty( $element->attributes['_root']['id'] ) ? $element->attributes['_root']['id'] : false;

	if ( 'section-page-header' === $html_id ) {
		return get_post_meta( $element->post_id, 'show_header_section', true );
	}

	return $render;
}, 10, 2 );

@luistinygod This is still not working for me. Just to make sure we are testing the exact same scenario - I am placing a Template element into a Page Template and assigning it #section-page-header. Using the exact same code I previously posted. I am still seeing the following results:

  1. Modify Page Template (Builder) => Template appears correctly based on preview page
  2. Modify Page (Builder) => Template DOES NOT appear on any pages despite logic
  3. Preview Page (Browser) => Template appears correctly for all pages

Hello @bmc38119

In your first post you said:

For my understanding, this means you have a section inside of the Header template that will be shown based on an ACF field → tested locally and it works.

On your last reply you say:

This is a different scenario. This is not a bug but how it was designed to work. In this scenario you are expecting to see parts of the page template when editing the page itself which you won’t see because when you edit a page with Bricks you only edit the page content, without previewing the page template surroundings. For example, if your page template has one Heading element and a Post Content element, when you edit the page content itself, you won’t see the Heading element from the Page Template (with or without any conditional display logic in place).