Archived topic
Applying Content Template to Post Type
3 replies · Started by EcoDev on May 8, 2021
Hello, I'm trying to setup a custom search results page using the new Content Template feature, I would like to apply a certain template when the post is a page and a certain template when it is a post or CPT, but I don't see an option in the display rules to apply to a certain post type (apply to post with terms etc).
Thanks
Hi there,
You can make use of this filter: generate_should_render_content_template
For example:
add_filter( 'generate_should_render_content_template', function( $display, $element_id ) {
// Update the below with the Element ID you're targeting.
if ( 123 === (int) $element_id ) {
if ( 'page' === get_post_type() ) {
$display = true;
} else {
$display = false;
}
}
}, 10, 2 );
That will make it so your Element with that ID (123 in this case) will only display if page is the post type.
Hope this helps!
Great, thanks Tom I'll give that a go.
PS may be a nice option in a future update for post type to be selectable in the user interface (along with taxonomies etc)?
Thanks
We'll definitely be expanding the available conditions :)