• Resolved MattDotCom

    (@mattdotcom)


    Hello! I’m trying to make a shortcode to use in archive and search results layouts that simply displays the name/label of the POST TYPE associated with a result.

    For instance, if I search for “Banana” I’ve already customized results to show results from all sorts of different post types (let’s call them “Colors” “Fruits” “Jokes”).

    Somewhere on the search results page, next to each post’s title, I want to display which of those 3 posts types that result comes from.

    I’ve tried the code below (found elsewhere in the forums) but it does not show anything:

    add_shortcode( 'post_type_title', 'nick1999_post_type_title' );
    function nick1999_post_type_title( $atts ) {
    	$atts = shortcode_atts( [
    		'post_type' => '',
    	], $atts );
    
    	if ( empty( $atts['post_type'] ) ) {
    		return '';
    	}
    
    	$obj = get_post_type_object();
    
    	return $obj->label;
    }
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    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.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Also you need to pass the post type into get_post_type_object() otherwise it returns null.

    Thread Starter MattDotCom

    (@mattdotcom)

    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?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    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.

    Thread Starter MattDotCom

    (@mattdotcom)

    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?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    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.

    Thread Starter MattDotCom

    (@mattdotcom)

    Worked like a charm Michael — thanks so much for your help and persistence!

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Awesomesauce!

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Make shortcode to display post type’s name/label’ is closed to new replies.