Site logo

Archived topic

Content Template Element - "Apply to" settings

1 reply · Started by Eric on August 8, 2022

Viewing posts 1–2 of 2

I'm a little confused about how to use the Apply To settings that are part of the Content Template Elements.

https://docs.generatepress.com/article/block-element-content-template/#settings

There is a setting to Apply to "Posts with custom field"

I assumed this meant that you could choose to conditionally display a content template based on a custom field's meta value. When the option is chosen, you are provided a text box to fill in "Post meta name" which I had assumed would be the field's name.

But there is no field available to add the value of the field to complete a conditional rule.

For example, what if I were to create a custom field named "content_template" with values corresponding to a few different standard page layouts. Am I misunderstanding what these settings are intended to be used for?

Hi Eric,

The setting allows the content template to display on posts that have that post meta with a value. If it’s null, it wont display.

It’s possible though to filter the content template to display on specific posts with a specific meta. You’ll need a snippet like this:

add_filter( 'generate_should_render_content_template', function( $display, $post_id ) {
	
	if(397867 === $post_id) {
		$my_meta = get_post_meta( get_the_ID(), 'my-co' );

		if( $my_meta ) {
			if ( $my_meta[0] === 'test-auth'  ) {
						$display = true;
			}   else {
				$display = false;
			}
		}
	}

	
return $display;

},10, 2 );

In this snippet, $my_meta is the post meta and test-auth is the meta value condition. Replace 397867 with the Block Element - Content template id as well.

Kindly modify the code to your preference. Set the “apply to” to all posts as well.

This archived topic is closed to new replies.