In WordPress, is_post_type_archive() returns true on the archive page of a post type, such as /books/ for a book post type.
The post type must have has_archive set to true when you register it. Call the tag with no argument to match any post type archive, or pass a name to match one.
Syntax
Use it as the test of an if statement.
WordPress
if ( is_post_type_archive() ) {
// code for the true path
} else {
// code for the false path
}
Call
What it does
is_post_type_archive()
True on any post type archive.
is_post_type_archive( 'book' )
True on the archive of the book post type.
is_post_type_archive( array( 'book', 'movie' ) )
True on either archive.
The theme file is archive-book.php for a post type named book.
On /books/ (true):
<main class="grid">
On other pages (false):
<main class="list">
How It Works
The short ? : form picks a class name.
esc_attr() makes the value safe.
Your CSS can style .grid as a card layout.
Frequently Asked Questions
It returns true or false (a bool). Use it inside an if statement.
Make sure has_archive is true in register_post_type() and that you flushed the permalinks.
No. A single custom post is checked with is_singular( 'book' ) or is_single().
No. The posts index uses is_home().
Conditional tags need the main query. Call them in template files or on hooks such as wp or template_redirect, not directly at the top of functions.php.