is_post_type_archive( string|string[] $post_types = '' ): bool
Determines whether the query is for an existing post type archive page.
Description
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
Parameters
-
$post_types
string|string[] Optional -
Post type or array of posts types to check against.
Default:
''
Return
bool Whether the query is for an existing post type archive page.
Source
File: wp-includes/query.php
.
View all references
function is_post_type_archive( $post_types = '' ) {
global $wp_query;
if ( ! isset( $wp_query ) ) {
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
return false;
}
return $wp_query->is_post_type_archive( $post_types );
}
Changelog
Version | Description |
---|---|
3.1.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
Example
If the current page is an archive of a custom post type, display the custom post type title:
Notes
This returns true for a page like
/?post_type=my-custom-post-type
, but not for/category/uncategorized/?post_type=custom
. It’s only testing whether this is an archive of all posts of a given type. It is not checking for the existence of thepost_type
query parameter — that can be found byget_query_var('post_type')
.Also, depending on when this function runs it may or may not be run by
nav_menu_item
. For example:Whether “Do stuff” gets run in the menu depends on whether the theme use nav menus. A better usage would be:
Conditionally enqueue (add) styles/scripts with custom post type archive page
Usage