I sometimes have no clue on which conditional to use is_singular(), is_post_type_archive() and what the post type is actually named for a certain plugin
is_singular()
will return true on any single post page
- This conditional tag checks if a singular post is being displayed, which is the case when one of the following returns true:
is_single()
,is_page()
oris_attachment()
. If the$post_types
parameter is specified, the function will additionally check if the query is for one of the post types specified.
is_post_type_archive()
will return true when the srchive page is being viewed of a custom post type
-Checks if the query is for an archive page of a given post type(s).
As for code, you can always use the $post
global and then use that to check the post type of the given post. On archive pages, $post
should always contain the post object of the last post in a query.
Something like following will work:
global $post;
// Get all the custom post types
$args = [
'public' => true,
'_builtin' => false
];
$post_types = get_post_types( $args );
$single_post_type = $post->post_type;
// For single post pages
if ( is_single() ) { // Or you can use is_singular()
// Only display post type name if post type is not build in type
if ( in_array( $single_post_type, $post_types ) ) {
echo $single_post_type;
}
}
// For single post type archives
if ( is_post_type_archive ) {
echo $single_post_type;
}