has_custom_logo()
云策文档标注
概述
has_custom_logo() 函数用于检测网站是否设置了自定义徽标。它支持多站点环境,通过检查主题修改值来判断徽标是否存在。
关键要点
- 函数返回布尔值,表示网站是否有自定义徽标
- 可选参数 $blog_id 用于指定多站点中的博客 ID,默认为当前博客
- 内部使用 get_theme_mod('custom_logo') 获取徽标附件 ID,并通过 wp_attachment_is_image() 验证是否为图像
- 在多站点切换时,会临时切换博客上下文以确保正确性
代码示例
// 检测当前博客是否有自定义徽标
if ( has_custom_logo() ) {
echo '自定义徽标已设置';
} else {
echo '未设置自定义徽标';
}
// 在多站点中检测指定博客 ID 的徽标
$blog_id = 2;
if ( has_custom_logo( $blog_id ) ) {
echo '博客 ' . $blog_id . ' 有自定义徽标';
}注意事项
- 函数自 WordPress 4.5.0 版本引入
- 相关函数包括 get_custom_logo()、the_custom_logo() 等,用于获取或输出徽标
- 在多站点环境中使用 $blog_id 参数时,需注意权限和上下文切换的影响
原文内容
Determines whether the site has a custom logo.
Parameters
$blog_idintoptional-
ID of the blog in question. Default is the ID of the current blog.
Source
function has_custom_logo( $blog_id = 0 ) {
$switched_blog = false;
if ( is_multisite() && ! empty( $blog_id ) && get_current_blog_id() !== (int) $blog_id ) {
switch_to_blog( $blog_id );
$switched_blog = true;
}
$custom_logo_id = get_theme_mod( 'custom_logo' );
$is_image = ( $custom_logo_id ) ? wp_attachment_is_image( $custom_logo_id ) : false;
if ( $switched_blog ) {
restore_current_blog();
}
return $is_image;
}
Changelog
| Version | Description |
|---|---|
| 4.5.0 | Introduced. |
Skip to note 3 content
Anthony Hortin
See Also
the_custom_logo()
get_custom_logo()
Skip to note 4 content
Rami Yushuvaev
To get the URL of the custom logo image:
$custom_logo_id = get_theme_mod( 'custom_logo' ); $image = wp_get_attachment_image_src( $custom_logo_id , 'full' ); echo $image[0];