How to display CPT’s singular_label in Bricks

Looking to get and output a post type’s singular label in Bricks on single CPT pages?

Define a custom function by adding the following in your child theme’s functions.php or a code snippets plugin:

function bl_get_post_type_singular_label() {
	$post_type_object = get_post_type_object( get_post_type() );
	
	return $post_type_object->labels->singular_name;
}

and then replace a Heading/Basic Text element’s default text with:

{echo:bl_get_post_type_singular_label}

where needed.

Welcome to {echo:bl_get_post_type_singular_label}-Post

for example, in a Bricks template that has been set to apply to either CPTs, would show

Welcome to Testimonial-Post

when viewing a testimonial custom post type’s singular page like https://bricks.local/testimonial/frances-harper/.

and

Welcome to Event-Post

when viewing a event CPT’s singular page like https://bricks.local/event/t20-cricket-world-cup/.

References

https://wordpress.stackexchange.com/a/271030/14380

https://developer.wordpress.org/reference/functions/get_post_type/

Get access to all 526 Bricks code tutorials with BricksLabs Pro

1 comment

Leave your comment