Site logo

Archived topic

How to display the Name of the Custom Post Type via Elements?

7 replies · Started by Sascha on May 31, 2022

Viewing posts 1–8 of 8

Hi,

please, allow me to handle this request here, since I use both GPP & GBP and would like to keep all my support tickets here.

I two CPTs "Academy" & "Support".

Now I want to hook the Name of the CPT on top of the post-title dynamically.

So when a visitor opens the post "How to learn french?" (Academy post), the name "Academy" will be placed between the header & the post-title.

I have created a block-hook element "after_header" and added a GBP-headline-block, which will be visible on all single posts of these two CPTs.

How can I use this headline to display the current CPT-name?

Thank you in advance and kind regards,
Sascha

Hi Sascha,

The post type name is not a regular value that you can simply call using a headline block.

Try this function and use this shortcode: [post_type_name]

add_shortcode('post_type_name',function() {
    ob_start();
    $post = get_queried_object();
    $postType = get_post_type_object(get_post_type($post));
        if ($postType) {
            echo esc_html($postType->labels->singular_name);
         } 	
    return ob_get_clean();
});

You can use a hook element and tick the execute shortcode box, or use a block element, add the shortcode into a shortcode block.

Thanks Ying,

I have added the code via Code Snippet and added the shortcode in Elements, as you can see on this screenshot. Here you can see the Element Type "Block Hook".

But the archive-title is still not visible, as you can see here & here.

Can you please help me further?

Thank you in advance and kind regards,
Sascha

Hi Sascha,

Can you try modifying the shortcode to something like this instead?

add_shortcode('post_type_name',function() {
    ob_start();
	global $post;
    $postType = get_post_type_object(get_post_type($post));
        if ($postType) {
            echo esc_html($postType->labels->singular_name);
         } 	
    return ob_get_clean();
});

Kindly let us know how it goes.

Hi Fernando, thanks a lot, I have replaced the previous shortcode with the one you have provided, but nothing has changed. Caching was disabled.

Sorry, some confusion here 🙈 I have two issues:

1. CTP Archive Page: How to Replace the "Archive:Academy" title with a custom title, which includes the Custom Post Type Name "Academy" dynamically?

Example of the title for the CTP "Academy": "Academy Overview" instead of "Archive:Academy"
Example of the title for the CTP "Support": "Support Articles" instead of "Archive:Support"

2. CTP Single Pages: Currently, I have added a breadcrumb via the shortcode [seopress_breadcrumbs]. I wonder if GP or GB (have Pro for both) can help me to remove the single-post-title from the breadcrumb?

Thank you in advance and kind regards,
Sascha

1. The shortcode above allows you to retrieve the Post type. You can use a Block Element - Page Hero, then display the post type dynamically by adding a Shortcode Block with shortcode - [post_type_name].

So we can modify the shortcode so that it conditionally checks the post type to display the subtitle as well:

add_shortcode('post_type_name',function() {
    ob_start();
	global $post;
    $postType = get_post_type_object(get_post_type($post));
        if ($postType) {
			if( 'Academy' === $postType->labels->singular_name) {
				echo '<h1 class="my-cpt-class">' . $postType->labels->singular_name . ' - Academy Overview</h1>';
			} else if( 'Support' === $postType->labels->singular_name) {
				echo '<h1 class="my-cpt-class">' . $postType->labels->singular_name . ' - Support Articles</h1>';
			} 
         } 	
    return ob_get_clean();
});

Sample usage: https://share.getcloudapp.com/kpu8y1Qq

Alternatively, you can also create two Block Elements - Page Hero for each CPT archive page.

2. I believe you can modify this in Yoast settings. See: https://yoast.com/help/the-breadcrumbs-title-setting/

Hope this clarifies!

This archived topic is closed to new replies.