钩子文档

get_custom_logo

💡 云策文档标注

概述

get_custom_logo 是一个 WordPress 过滤器,用于修改自定义徽标的 HTML 输出。它允许开发者通过过滤函数调整徽标的显示方式,例如移除特定属性以解决结构化数据问题。

关键要点

  • 过滤器名称:get_custom_logo
  • 参数:$html(自定义徽标的 HTML 输出字符串)和 $blog_id(博客 ID,用于多站点环境)
  • 用途:过滤自定义徽标的输出,常用于调整徽标链接或属性
  • 相关函数:get_custom_logo(),位于 wp-includes/general-template.php
  • 版本历史:在 WordPress 4.5.0 引入,4.6.0 添加了 $blog_id 参数

代码示例

add_filter( 'get_custom_logo', 'wecodeart_com' );
// Filter the output of logo to fix Googles Error about itemprop logo
function wecodeart_com() {
    $custom_logo_id = get_theme_mod( 'custom_logo' );
    $html = sprintf( '%2$s',
        esc_url( home_url( '/' ) ),
        wp_get_attachment_image( $custom_logo_id, 'full', false, array(
            'class'    => 'custom-logo',
        ) )
    );
    return $html;
}

注意事项

此过滤器可用于解决 Google 结构化数据测试工具中关于 itemprop "logo" 的错误,通过移除该属性来优化 SEO 兼容性。


📄 原文内容

Filters the custom logo output.

Parameters

$htmlstring
Custom logo HTML output.
$blog_idint
ID of the blog to get the custom logo for.

Source

return apply_filters( 'get_custom_logo', $html, $blog_id );

Changelog

Version Description
4.6.0 Added the $blog_id parameter.
4.5.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    If you test your site with Google’s Structured data testing tool you will get an error about itemprop “logo” not being recognized by google as a WP Header element.
    Here is how you remove itemprop

    add_filter( 'get_custom_logo', 'wecodeart_com' );
    // Filter the output of logo to fix Googles Error about itemprop logo
    function wecodeart_com() {
    	$custom_logo_id = get_theme_mod( 'custom_logo' );
    	$html = sprintf( '<a href="%1$s" class="custom-logo-link" rel="home" itemprop="url">%2$s</a>',
                esc_url( home_url( '/' ) ),
                wp_get_attachment_image( $custom_logo_id, 'full', false, array(
                    'class'    => 'custom-logo',
                ) )
            );
    	return $html;	
    }