WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Using the custom post type label and description (9 posts)

  1. Ali Gledhill
    Member
    Posted 4 years ago #

    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.

  2. Ali Gledhill
    Member
    Posted 4 years ago #

    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!

  3. hixen
    Member
    Posted 4 years ago #

    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?

  4. lehooo
    Member
    Posted 3 years ago #

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

  5. lancemonotone
    Member
    Posted 3 years ago #

    @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:"
      ["edit"]=>"Edit"
      ["view"]=>"View Press Release"
  6. Daniele Raimondi
    Member
    Posted 3 years ago #

    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.

  7. johnfrancisco
    Member
    Posted 3 years ago #

    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 ; ?>

    Thks

  8. Kic00
    Member
    Posted 3 years ago #

    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?

  9. johnfrancisco
    Member
    Posted 3 years ago #

    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.)

Topic Closed

This topic has been closed to new replies.

About this Topic