Hi stackflow community,
I’m fairly new to PHP, so please excuse any mistakes.
For WordPress I have registered a custom post type called (‘energy’) (see code).
I know that with the command <PHP echo get_post_type();><?/>.
I can call the name of the post type and I get energy
as a result. But I want to call the 'name'
that is situated in the 'labels'
array. The Energy & Transport
– how do I do that?
I read it’s possible with get_post_type_object();
but, a) I don’t know if that’s true and b) I don’t know how to construct the correct command.
Can anyone please help me to get the correct command? Thanks in advance.
CUSTOM POST TYPE:
register_post_type('energy', array(
'public' => true,
'show_in_rest' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail'),
'menu_icon' => 'dashicons-lightbulb',
'taxonomies' => array( 'category' ),
'labels' => array(
'name' => 'Energy & Transport',
'add_new_item' => 'Add New Post for Energy & Transport',
'all_items'=> 'All Energy & Transport Posts',
)
));
3
Answers
So you just need to learn a bit more about objects and arrays really.
You are using the right function
get_post_type_object();
In order to access the labels you need to descend the object like so
Here is the documentation on objects:
https://www.php.net/manual/en/language.types.object.php
You can use a combination of
get_post_type()
andget_post_type_object()
https://developer.wordpress.org/reference/functions/get_post_type/
https://developer.wordpress.org/reference/functions/get_post_type_object/
You can try with the below code –