Show post type name in Search Results

I have a post grid set up on my Search Results page, and I want to show what post type each result belongs to. I followed the advice from this post and got it working, but it’s showing the slug rather than the name (e.g. “news-release” rather than “News Release”). Any idea how I could have it show the full name?

This is what I did following the advice in the linked post:

Added this to theme functions file:

add_filter( ‘bricks/code/echo_function_names’, function() {
return [
‘get_post_type’,
];
} );

Then added this to a text module: {echo:get_post_type}

Thanks!

Here are some possible solutions. Credits to claude.

  1. Using get_post_type_object():
add_filter( 'bricks/code/echo_function_names', function() { 
    return [ 'get_post_type_object', ]; 
} );

Then in your text module, use:

{echo:get_post_type_object().labels.name}
  1. If you prefer a custom function, you can add this to your theme’s functions.php:
function get_pretty_post_type_name($post_type = null) {
    if ($post_type === null) {
        $post_type = get_post_type();
    }
    $post_type_obj = get_post_type_object($post_type);
    return $post_type_obj ? $post_type_obj->labels->name : $post_type;
}

add_filter( 'bricks/code/echo_function_names', function() { 
    return [ 'get_pretty_post_type_name', ]; 
} );

Then in your text module:

{echo:get_pretty_post_type_name()}

The first method directly uses WordPress’s built-in get_post_type_object() function to retrieve the post type’s labels. The second method creates a custom function that provides a bit more flexibility and error handling.

Both approaches will convert slugs like “news-release” to their more readable names like “News Release”. The key is using the labels.name property of the post type object, which contains the user-friendly name.

Choose whichever method feels more comfortable to you. The second method (custom function) might be slightly more robust if you anticipate any edge cases.

1 Like

Thank you so much for your help and detailed explanation! The second method worked perfectly.

1 Like

Super helpful @emmanuelson !
Is it possible to get pull a singular version of the pretty name?

I think bricks has a dynamic tag for that {post_type}