WordPress.org

Ready to get started?Download WordPress

Codex

Function Reference/is post type archive

Contents

Description

Checks if the query is for an archive page of a given post type(s).

Usage

<?php is_post_type_archive$post_types ); ?>

Parameters

$post_types
(array/string) (optional) Post type or array of posts types to check against.
Default: None

Return Values

(boolean) 
Whether the query is for an archive page of a given post type(s).

Examples

Determine if the current page is an archive made by a custom post type and display the custom post type title:

<?php
if ( is_post_type_archive() ) {
    ?>
    <h1><?php post_type_archive_title(); ?></h1>
    <?php
}
?>

Notes

This returns false for a page like /category/uncategorized/?post_type=custom. It only returns true for a page like /?post_type=custom. In other words, it's not checking for the existence of the post_type query parameter, it's testing if this is the archive of all posts of a given type. You might instead be looking for get_query_var('post_type').

Depending on when this function is run it may or may not be run by nav_menu_item. Take the following code example:

<?php
function some_func( $query ){
    if ( is_post_type_archive('my_custom_post_type') ) {
         // Do stuff
    }
}
add_action('pre_get_posts','some_func');
?>

"Do stuff" may or may not be run in the menu, depending on if the theme is using nav menus or not. The correct usage would be:

<?php
function some_func( $query ){
    if ( is_post_type_archive('my_custom_post_type') 
         && ! empty( $query->query['post_type']  == 'my_custom_post_type' ) {
         // Do stuff
    }
}
add_action('pre_get_posts','some_func');
?>

Change Log

Source File

is_post_type_archive() is located in wp-includes/query.php.

Related

Post Types: register_post_type(), add_post_type_support(), remove_post_type_support(), post_type_supports(), post_type_exists(), set_post_type(), get_post_type(), get_post_types(), get_post_type_object(), get_post_type_capabilities(), get_post_type_labels(), is_post_type_hierarchical(), is_post_type_archive(), post_type_archive_title()

See also index of Function Reference and index of Template Tags.