函数文档

is_favicon()

💡 云策文档标注

概述

is_favicon() 是一个 WordPress 条件查询标签函数,用于检测当前查询是否针对 favicon.ico 文件。它返回布尔值,并依赖于全局 $wp_query 对象。

关键要点

  • 函数返回布尔值,指示查询是否为 favicon.ico 文件。
  • 在查询运行前调用会触发 _doing_it_wrong() 警告并返回 false。
  • 内部通过 $wp_query->is_favicon() 实现检测逻辑。
  • 首次引入于 WordPress 5.4.0 版本。

代码示例

function is_favicon() {
    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_favicon();
}

注意事项

  • 避免在查询运行前使用此函数,否则会返回 false 并记录错误。
  • 相关函数包括 WP_Query::is_favicon()、__() 和 _doing_it_wrong()。
  • 在 WP::handle_404() 和 redirect_canonical() 等函数中被调用。

📄 原文内容

Is the query for the favicon.ico file?

Return

bool Whether the query is for the favicon.ico file.

Source

function is_favicon() {
	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_favicon();
}

Changelog

Version Description
5.4.0 Introduced.