Using WordPress ‘get_post_type_labels()’ PHP function

The get_post_type_labels() WordPress PHP function builds an object with all post type labels out of a post type object.

Usage

The function is used like this:

$post_type_labels = get_post_type_labels( $post_type_object );

In this example, $post_type_object is the post type object you want to retrieve the labels for. The function will return an object containing all the labels for the post type.

Parameters

  • $post_type_object (object|WP_Post_Type – Required): The post type object for which the labels are to be returned.

More information

See WordPress Developer Resources: get_post_type_labels()

This function is available since WordPress version 3.0.0. To get a post type labels and other registered parameters, use the get_post_type_object() function instead.

Examples

Get Labels for a Custom Post Type

// Assuming you have a custom post type 'books'
$post_type_object = get_post_type_object( 'books' );
$labels = get_post_type_labels( $post_type_object );
echo $labels->name; // Outputs 'Books'

This code retrieves the labels for the ‘books’ post type and then echoes the general name for the post type, which is ‘Books’.

Get Singular Name for a Post Type

$post_type_object = get_post_type_object( 'books' );
$labels = get_post_type_labels( $post_type_object );
echo $labels->singular_name; // Outputs 'Book'

This example retrieves the singular name for the ‘books’ post type, which is ‘Book’.

Get Label for Adding New Item

$post_type_object = get_post_type_object( 'books' );
$labels = get_post_type_labels( $post_type_object );
echo $labels->add_new_item; // Outputs 'Add New Book'

In this example, it retrieves the label for adding a new book, which is ‘Add New Book’.

Get Label for Editing Item

$post_type_object = get_post_type_object( 'books' );
$labels = get_post_type_labels( $post_type_object );
echo $labels->edit_item; // Outputs 'Edit Book'

Here, it retrieves the label for editing a book, which is ‘Edit Book’.

Get Label for Viewing Item

$post_type_object = get_post_type_object( 'books' );
$labels = get_post_type_labels( $post_type_object );
echo $labels->view_item; // Outputs 'View Book'

Finally, this example retrieves the label for viewing a book, which is ‘View Book’.