返回介绍

is_post_type_archive()

发布于 2017-09-11 01:23:03 字数 4022 浏览 1121 评论 0 收藏 0

is_post_type_archive( string|array $post_types = '' )

Is the query for an existing post type archive page?


description


参数

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

更新日志

Versiondescription
3.1.0Introduced.

相关函数

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' );
    ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文