Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
    
You got a custom post type, that's registered with the name of "Pretty Little Liars"? –  kaiser Jul 31 '12 at 10:52
    
Yes i do, I'm opening a blog thats all about TV shows, can you help me? –  Terrell Anderson Jul 31 '12 at 11:04
    
Already answered, but honestly: Why not simply add taxonomies for the different shows? –  kaiser Jul 31 '12 at 11:10
    
I wanted to add Post types because each post type page will have a design to fit it :) –  Terrell Anderson Jul 31 '12 at 11:13
    
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 Jul 31 '12 at 11:26
add comment

1 Answer

up vote 3 down vote accepted

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( $pts, '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().

share|improve this answer
    
Hey Thank you very much Kaiser, but i just found a more simple solution, using this code <?php $post_type = get_post_type_object( get_post_type($post) ); echo $post_type->label ; ?> It shows the post type name but the only problem is, i don't know how to get it to link to the post type page, can you help me with that one please? –  Terrell Anderson Jul 31 '12 at 11:14
    
First: Don't mess with objects, until you really know what you're doing. The public API (a.k.a. "Template Tags") have a reason why they exist. If you don't want that the sky falls on your head with an update, then use what ↑ I recommended/wrote for you. Second: It should be as simple as using the the_permalink() Template Tag. –  kaiser Jul 31 '12 at 11:27
    
So your code is calling all the post type titles right? and if so how do i get them to display on the homepage, on a post that's associated with the post type –  Terrell Anderson Jul 31 '12 at 11:31
    
Simply add wpse60306_get_post_type() in your loop. –  kaiser Jul 31 '12 at 11:49
    
Thank you very much Kaiser :) I'm hoping to launch the site in mid august Tv-Cafe.com –  Terrell Anderson Jul 31 '12 at 11:52
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.