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 |2 Answers
Reset to default 6You 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
版权声明:本文标题:How do i display the post type title? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741577954a2386399.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
body_class()
andpost_class()
) to your rules... – kaiser Commented Jul 31, 2012 at 11:26