If you’re using this shortcode in the php template files, you’ll want to make sure you’re doing <?php echo do_shortcode( '[post_type_title post_type="fruits"]' ); ?> and so on.
Also you need to pass the post type into get_post_type_object() otherwise it returns null.
Thanks for your replies Micael! I am back to this puzzle for another client.
I’m using the shortcodes in a Beaver Builder / Beaver Themer context, so do not need to add them into template files.
Forgive my ignorance, but when you say “pass the post type” into that get_post_type_object() can you be more specific?
For example:
$obj = get_post_type_object( 'fruits' );
You’re not telling the function which post type object to retrieve. Make sure to use the post type’s slug.
Thank you! Let me clarify — I want it to retrieve the post type of the post it is… retrieving. WOW that was circular. Let me be more specific:
I’m using Beaver Builder and Beaver Themer to make this search results archive: https://doublebasshq.com/?s=wood
I want, alongside each item’s author and title, to display what POST TYPE that entry is (“Learn” “Gear” “Lounge” and “Event Reviews” are all separate post types) so that the theme of that content is clear.
SO in the PowerPack content grid, I am placing [post_type_title] within the content there alongside other shortcodes:
<div class="pp-content-grid-post-text">
<h3 class="pp-content-grid-post-title">[wpbb post:link text="title"]</h3>
<div class="pp-content-grid-post-meta">
By [publishpress_authors_data field="display_name"] · [wpbb post:author_bio]
<br>[post_type_title]
</div>
</div>
But currently, my [post_type_title] is just displaying blank, probably for the reason you point out in my functions.php — so how do I tell it to retrieve that post type label of whatever that particular result is?
Perhaps a better approach, given that information would be to try out this:
add_shortcode( 'post_type_title', 'nick1999_post_type_title' );
function nick1999_post_type_title( $atts ) {
$current_post_post_type = get_post_type( get_the_ID() );
$obj = get_post_type_object( $current_post_post_type );
return $obj->label;
}
Hopefully this would get the current post ID being iterated over, and then fetch the post type from that post, and then fetch the post type object and return the label value.
Worked like a charm Michael — thanks so much for your help and persistence!