Support » How-To and Troubleshooting » [Resolved] Using the custom post type label and description

[Resolved] Using the custom post type label and description

  • I think this should be easy to solve, I just can’t find any relevant examples in the Codex or by searching around on Google. I have set up some custom post types on my site, and everything is working nicely (nice feature!). I want to display the custom post type label within the loop, but I can’t find how to do so.

    I am currently using <?php echo get_post_type($post) ; ?> which prints the post type name (eg “reviews”). But I want to publish the label, or singular label, (eg “Reviews” or “Review”). Likewise, I want to publish the post type description. Can anyone suggest the code I would use?

    Many thanks.

Viewing 8 replies - 1 through 8 (of 8 total)
  • After a lot of work, I figured it out:

    <?php $post_type = get_post_type_object( get_post_type($post) );
    echo $post_type->label ; ?>

    It took some detective work, but Justin Tadlock’s post gave a good enough hint!

    Great work Ali, I have been looking for this.

    But I do have to ask, did you ever in your work figure out how to output the label as a link, kinda work the same way as a category link, but showing all custom post types in that label?

    I just wanted to add that for those who want to access the singular label, it can be done through:
    I suspect that all the other labels can be accessed in the same way, but I haven’t tried it out yet myself.



    @lehooo is correct.

    This is an example of what $post_type->labels->{term} gives you:

    ["name"]=>"Press Releases"
      ["singular_name"]=>"Press Release"
      ["add_new"]=>"Add New"
      ["add_new_item"]=>"Add New Press Release"
      ["edit_item"]=>"Edit Press Release"
      ["new_item"]=>"New Press Release"
      ["view_item"]=>"View Press Release"
      ["search_items"]=>"Search Press Releases"
      ["not_found"]=>"No Press Releases Found"
      ["not_found_in_trash"]=>"No Press Releases Found In Trash"
      ["parent_item_colon"]=>"Parent Press Releases:"
      ["view"]=>"View Press Release"

    Ali method works great, but it relies on data extracted from the first post ($post, out of the loop) of the current type. I think a more reasonable way, is to use

    $post_type = $wp_query->get_queried_object()

    it does exactly what its name says, it returns the queried object for the current context (in this case, the current custom post type object) and it works in many other contexts such as pages, categories etc etc.

    Thanks Ali and lehooo.
    I use both your methods and it works fine.

    <?php $post_type = get_post_type_object( get_post_type($post) );
    		echo $post_type->labels->singular_name ; ?>


    Weird – I tried to assign a variable then change the value but it always returns “Post”, as in:
    $foo = $post_type->labels->singular_name ;
    if ($foo = “Post”) { $foo = “Bar”; } else { echo $foo; }
    echo $foo <- always returns Bar, no matter if there’s another singular label.

    Any clues?

    Hi Kic00

    I guess the mistake is confusion between php operator = (assign) and == (compare).

    try this :

    $foo = $post_type->labels->singular_name ;
    if ($foo == "Post") { $foo = "Bar"; } else { echo $foo; }
    echo $foo

    (If your post is a post, it will return “Bar” as you assign it, else it will echo the singular name of label of your custom post type.)

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Resolved] Using the custom post type label and description’ is closed to new replies.