is_post_type_archive() - WordPress 函数参考中文文档

返回介绍

is_post_type_archive()

发布于 2017-09-11 字数4024 浏览 928 评论 0

is_post_type_archive( string|array $post_types = '' )

Is the query for an existing post type archive page?


描述


参数

$post_types

(string|array)
(Optional)
Post type or array of posts types to check against.

Default value: ”


返回值

(bool)


源代码

File: wp-includes/query.php

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 );
}

更新日志

Version 描述
3.1.0 Introduced.

相关函数

Uses

  • wp-includes/l10n.php:
    __()
  • wp-includes/class-wp-query.php:
    WP_Query::is_post_type_archive()
  • wp-includes/functions.php:
    _doing_it_wrong()

Used By

  • wp-includes/general-template.php:
    wp_get_document_title()
  • wp-includes/general-template.php:
    get_the_archive_title()
  • wp-includes/general-template.php:
    feed_links_extra()
  • wp-includes/general-template.php:
    wp_title()
  • wp-includes/general-template.php:
    post_type_archive_title()
  • wp-includes/class-wp.php:
    WP::handle_404()
  • wp-includes/nav-menu-template.php:
    _wp_menu_item_classes_by_context()
  • wp-includes/post-template.php:
    get_body_class()
  • Show 3 more used by
    Hide more used by


User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Usage

    <?php is_post_type_archive( $post_types ); ?>
  2. Example
    If the current page 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 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 the post_type query parameter — that can be found by get_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 my_function( $query ){
        if ( is_post_type_archive( 'my_custom_post_type' ) ) {
             // Do stuff
        }
    }
    add_action( 'pre_get_posts', 'my_function' );
    ?>

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

    <?php
    function my_function( $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', 'my_function' );
    ?>

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!