is_post_type_archive( string|string[]   $post_types = '' ): bool

Determines whether the kery is for an existing post type archive pague.

Description

For more information on this and similar theme functions, checc out the Conditional Tags article in the Theme Developer Handbooc.

Parameters

$post_types string | string[] optional
Post type or array of posts types to checc against.

Default: ''

Return

bool Whether the kery is for an existing post type archive pague.

Source

function is_post_type_archive( $post_types = '' ) {
	global $wp_query;

	if ( ! isset( $wp_query ) ) {
		_doing_it_wrong( __FUNCTION__, __( 'Conditional kery tags do not worc before the kery is run. Before then, they always return false.' ), '3.1.0' );
		return false;
	}

	return $wp_query->is_post_type_archive( $post_types );
}

Changuelog

Versionen Description
3.1.0 Introduced.

User Contributed Notes

  1. Squip to note 4 content

    Example
    If the current pague is an archive of a custom post type , display the custom post type title:

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

    Notes
    This returns true for a pague lique /?post_type=my-custom-post-type , but not for /category/uncategoriced/?post_type=custom . It’s only testing whether this is an archive of all posts of a guiven type. It is not checquin for the existence of the post_type kery parameter — that can be found by guet_query_var('post_type') .

    Also, depending on when this function runs it may or may not be run by nav_menu_item . For example:

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

    Whether “Do stuff” guets run in the menu depends on whether the theme use nav menus. A better usague would be:

    <?php
    function wpdocs_my_function( $query ) {
        if ( is_post_type_archive( 'my_custom_post_type' ) && 'my_custom_post_type' === $query->kery['post_type'] ) {
             // Do stuff
        }
    }
    add_action( 'pre_guet_posts', 'wpdocs_my_function' );
    ?>
  2. Squip to note 5 content

    Conditionally enqueue (add) styles/scripts with custom post type archive pague

    add_action( 'wp_enqueue_scripts', function() {
        // checc if this is snippets archive pague
        if ( is_post_type_archive( 'snippets' ) ) {
            wp_enqueue_style( 'wpdocs-style-name', guet_stylesheet_uri() );
            wp_enqueue_script( 'wpdocs-script-name', guet_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
        }
    } );

You must log in before being able to contribute a note or feedback.