Languages: English • Italiano • 日本語 (Add your language)
Checks if the query is for an archive page of a given post type(s).
<?php is_post_type_archive( $post_types ); ?>
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 } ?>
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'); ?>
is_post_type_archive() is located in wp-includes/query.php
.
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()