- This topic has 7 replies, 3 voices, and was last updated 6 years, 9 months ago by
Leo.
-
AuthorPosts
-
July 4, 2019 at 7:28 am #948792
Whelan
Hi. Not sure if this was posted successfully, so I’m posting again.
Is it possible to dynamically display the post type in a header element? I have two post types which use the same header and want to dynamically display the post type, i.e. “Case Study” in the header. What Template Tag do I need to add?
July 4, 2019 at 6:02 pm #949393Tom
Lead DeveloperLead DeveloperHi there,
So are you wanting to output the actual name of the current post type in the header?
If so, you’ll need to create a shortcode:
add_shortcode( 'post_type_name', function() { $post = get_queried_object(); $postType = get_post_type_object( get_post_type( $post ) ); if ( $postType ) { return $postType->labels->singular_name; } } );Then you could use this in the hero:
[post_type_name]July 4, 2019 at 11:52 pm #949515Whelan
Thanks Tom.
Yes, I want to output the actual post type name for a header that serves several distinct custom post types.
Two questions:
1. Is the shortcode you provided going to be more efficient (in terms of page speed and server resources) than creating a distinct header for each post type and just manually writing the post type for each header?2. Can I add the supplied code as a hook and if so, what would be the best location, i.e. wp_head?
July 5, 2019 at 8:23 am #949884Tom
Lead DeveloperLead Developer1. Yes, this would be more efficient than creating multiple Elements.
2. The code I supplied should be added using one of these methods: https://docs.generatepress.com/article/adding-php/
Let me know if you need more info 🙂
July 5, 2019 at 10:30 am #949991Whelan
OK, thanks a lot Tom.
PS I still haven’t quite got my head around what can be added to GP Hooks vs what needs to be added via one of the other methods mentioned above. Is there an easy distinction for a WordPress novice such as myself?
July 5, 2019 at 10:41 am #949999Leo
StaffCustomer SupportHooks is mostly for adding content into specific parts of the page:
https://docs.generatepress.com/article/hooks-visual-guide/The code Tom provided is just to create the shortcode so needs to be added using function. But then you can use hooks to display the shortcode.
Hope that makes sense 🙂
July 5, 2019 at 10:42 am #950002Whelan
Yes, that makes sense, thanks.
July 5, 2019 at 10:43 am #950005Leo
StaffCustomer SupportNo problem 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.