admin管理员组

文章数量:1293407

Hi i was wondering if there is a way that i can display the custom post type title.

For example:

I have a custom post type entitled "Pretty Little Liars" and it also shows on the homepage, but how do i get the POST TYPE title, not the title of the post to show up like a category.

For example:

Plublished in: Pretty Little Liars

is this possible?

Also i want it to link to the post type page

Hi i was wondering if there is a way that i can display the custom post type title.

For example:

I have a custom post type entitled "Pretty Little Liars" and it also shows on the homepage, but how do i get the POST TYPE title, not the title of the post to show up like a category.

For example:

Plublished in: Pretty Little Liars

is this possible?

Also i want it to link to the post type page

Share Improve this question asked Jul 31, 2012 at 10:50 Terrell AndersonTerrell Anderson 3613 gold badges7 silver badges16 bronze badges 5
  • You got a custom post type, that's registered with the name of "Pretty Little Liars"? – kaiser Commented Jul 31, 2012 at 10:52
  • Yes i do, I'm opening a blog thats all about TV shows, can you help me? – Terrell Anderson Commented Jul 31, 2012 at 11:04
  • Already answered, but honestly: Why not simply add taxonomies for the different shows? – kaiser Commented Jul 31, 2012 at 11:10
  • I wanted to add Post types because each post type page will have a design to fit it :) – Terrell Anderson Commented Jul 31, 2012 at 11:13
  • 1 You could conditionally switch templates. Or stylesheets. Or just append the class of the current taxonomy (see body_class() and post_class()) to your rules... – kaiser Commented Jul 31, 2012 at 11:26
Add a comment  | 

2 Answers 2

Reset to default 6

You can write a general template tag for this task.

function wpse60306_get_post_type( $echo = true )
{
    static $post_types, $labels = '';

    // Get all post type *names*, that are shown in the admin menu
    empty( $post_types ) AND $post_types = get_post_types( 
        array( 
            'show_in_menu' => true,
            '_builtin'     => false,
        ),
        'objects'
    );

    empty( $labels ) AND $labels = wp_list_pluck( $post_types, 'labels' );
    $names = wp_list_pluck( $labels, 'singular_name' );
    $name = $names[ get_post_type() ];

    // return or print?
    return $echo ? print $name : $name;
}

Explanation

We got two variables declared as static, so we don't have to redo the task, if you're for example using it inside a loop that shows posts from different post types.

You also got an argument ((bool) true/false) to switch if you just want to return or right print the name.

This function doesn't work for built in post types (assuming you don't need it). If you need it for built in post types too, then just remove the _builtin argument from the function inside ↑ get_post_types().

Just in case someone is looking for further answers here, there is a builtin wordpress function:

post_type_archive_title();

its there since 2011, reference: https://developer.wordpress/reference/functions/post_type_archive_title/, old reference: https://codex.wordpress/Function_Reference/post_type_archive_title

本文标签: How do i display the post type title