函数文档

the_custom_logo()

💡 云策文档标注

概述

the_custom_logo() 是 WordPress 中用于显示自定义徽标的模板函数,它会输出一个链接到首页的徽标,除非主题支持在首页移除该链接。此函数基于 get_custom_logo() 实现,适用于多站点环境。

关键要点

  • 函数功能:显示自定义徽标,通常链接到首页,但链接行为可受主题支持影响。
  • 参数:可选参数 $blog_id(整数),用于指定博客 ID,默认为当前博客。
  • 相关函数:与 get_custom_logo() 和 has_custom_logo() 关联,前者返回徽标 HTML,后者检查徽标是否存在。
  • 版本历史:自 WordPress 4.5.0 版本引入。

代码示例

// 获取自定义徽标图像的 URL
$custom_logo_id = get_theme_mod( 'custom_logo' );
$image = wp_get_attachment_image_src( $custom_logo_id , 'full' );
echo $image[0];

注意事项

  • 用户贡献笔记提供了扩展用法,例如将徽标添加到登录页面或修改登录链接。
  • 使用前应确保主题已启用自定义徽标功能,并检查 has_custom_logo() 以避免错误。

📄 原文内容

Displays a custom logo, linked to home unless the theme supports removing the link on the home page.

Parameters

$blog_idintoptional
ID of the blog in question. Default is the ID of the current blog.

Source

function the_custom_logo( $blog_id = 0 ) {
	echo get_custom_logo( $blog_id );
}

Changelog

Version Description
4.5.0 Introduced.

User Contributed Notes

  1. Skip to note 6 content

    Add your custom logo to the login page:

    function wpdev_filter_login_head() {
    
    	if ( has_custom_logo() ) :
    
    		$image = wp_get_attachment_image_src( get_theme_mod( 'custom_logo' ), 'full' );
    		?>
    		<style type="text/css">
    			.login h1 a {
    				background-image: url(<?php echo esc_url( $image[0] ); ?>);
    				-webkit-background-size: <?php echo absint( $image[1] )?>px;
    				background-size: <?php echo absint( $image[1] ) ?>px;
    				height: <?php echo absint( $image[2] ) ?>px;
    				width: <?php echo absint( $image[1] ) ?>px;
    			}
    		</style>
    		</pre>
    				</div><!-- .comment-content -->
    
    					<section id='feedback-2037' class='wporg-has-embedded-code feedback hide-if-js' data-comment-count='0'>
    </section><!-- .feedback -->
    <footer class='feedback-links wporg-dot-link-list' >
    <a role="button" class="feedback-login" href="https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fthe_custom_logo%2F%3Freplytocom%3D2037%23feedback-editor-2037" rel="nofollow">Log in to add feedback</a></footer>
    </article><!-- .comment-body -->
    </li>
    			<li id="comment-1793" data-comment-id="1793" class="comment byuser comment-author-ahortin even thread-even depth-1">
    			<article id="div-comment-1793" class="comment-body">
    
    							<a href="#comment-content-1793" class="screen-reader-text">Skip to note 7 content</a>
    				<header class="comment-meta">
    					<div class="comment-author vcard">
    						<span class="comment-author-attribution">
    						<a href="https://profiles.wordpress.org/ahortin/" rel="external nofollow" class="url">Anthony Hortin</a>						</span>
    						<a class="comment-date" href="https://developer.wordpress.org/reference/functions/the_custom_logo/#comment-1793">
    							<time datetime="2016-08-30T05:53:36+00:00">
    							10 years ago							</time>
    						</a>
    
    																													</div>
    					<div class="user-note-voting" data-nonce="cb78a41e17" data-can-vote="false"><a class="user-note-voting-up" title="You must log in to vote on the helpfulness of this note" data-id="1793" data-vote="up" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fthe_custom_logo%2F%23comment-1793"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a><span class="user-note-voting-count " title="75% like this"><span class="screen-reader-text">Vote results for this note: </span>4</span><a class="user-note-voting-down" title="You must log in to vote on the helpfulness of this note" data-id="1793" data-vote="down" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fthe_custom_logo%2F%23comment-1793"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a></div>				</header>
    				<!-- .comment-metadata -->
    			
    				<div class="wporg-has-embedded-code comment-content" id="comment-content-1793">
    				<p>See Also<br />
    <a href="https://developer.wordpress.org/reference/functions/get_custom_logo/">get_custom_logo()</a><br />
    <a href="https://developer.wordpress.org/reference/functions/has_custom_logo/">has_custom_logo()</a></p>
    				</div><!-- .comment-content -->
    
    					<section id='feedback-1793' class='wporg-has-embedded-code feedback hide-if-js' data-comment-count='0'>
    </section><!-- .feedback -->
    <footer class='feedback-links wporg-dot-link-list' >
    <a role="button" class="feedback-login" href="https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fthe_custom_logo%2F%3Freplytocom%3D1793%23feedback-editor-1793" rel="nofollow">Log in to add feedback</a></footer>
    </article><!-- .comment-body -->
    </li>
    			<li id="comment-3558" data-comment-id="3558" class="comment byuser comment-author-jackstron odd alt thread-odd thread-alt depth-1">
    			<article id="div-comment-3558" class="comment-body">
    
    							<a href="#comment-content-3558" class="screen-reader-text">Skip to note 8 content</a>
    				<header class="comment-meta">
    					<div class="comment-author vcard">
    						<span class="comment-author-attribution">
    						<a href="https://profiles.wordpress.org/jackstron/" rel="external nofollow" class="url">jackstron</a>						</span>
    						<a class="comment-date" href="https://developer.wordpress.org/reference/functions/the_custom_logo/#comment-3558">
    							<time datetime="2020-01-10T12:35:20+00:00">
    							6 years ago							</time>
    						</a>
    
    																													</div>
    					<div class="user-note-voting" data-nonce="78170142fe" data-can-vote="false"><a class="user-note-voting-up" title="You must log in to vote on the helpfulness of this note" data-id="3558" data-vote="up" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fthe_custom_logo%2F%23comment-3558"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a><span class="user-note-voting-count " title="100% like this"><span class="screen-reader-text">Vote results for this note: </span>2</span><a class="user-note-voting-down" title="You must log in to vote on the helpfulness of this note" data-id="3558" data-vote="down" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fthe_custom_logo%2F%23comment-3558"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a></div>				</header>
    				<!-- .comment-metadata -->
    			
    				<div class="wporg-has-embedded-code comment-content" id="comment-content-3558">
    				<p>Regarding the above ^^ example. Add the code below to also remove the link to wordpress.org with your homepage link.</p>
    <pre class="wp-block-code"><code lang="php" class="language-php ">function new_wp_login_url() {
    	return home_url();
    }
    add_filter('login_headerurl', 'new_wp_login_url');