the_custom_logo() の使い方|説明・引数・注意点

説明

the_custom_logo() は、カスタマイザーで設定したサイトロゴを出力する関数です。

基本構文

the_custom_logo( int $blog_id = 0 ): void

引数

  • $blog_id:サイトID(マルチサイト用)

戻り値

戻り値はありません(出力します)。

使い方(サンプル)

if ( has_custom_logo() ) {
	the_custom_logo();
}

注意点

  • add_theme_support( 'custom-logo' ) が必要です。

実務での使いどころ

  • カスタマイザーで設定した、サイトのロゴを、ヘッダーに出力するとき。

よくあるミスと対処

  • テーマが、add_theme_support( 'custom-logo' ) を、有効にしていない。ロゴを設定できない。
  • ロゴが、未設定のときに、何も出力されない。代わりの表示が必要なら、has_custom_logo() で、分岐する。
  • 値として使いたいのに、これを使っている。値は get_custom_logo()。

NG例

the_custom_logo(); // ロゴが未設定のときは、何も出ない

OK例

if ( has_custom_logo() ) {
	the_custom_logo();
} else {
	echo '<a href="' . esc_url( home_url( '/' ) ) . '">' . esc_html( get_bloginfo( 'name' ) ) . '</a>';
}

使い分け早見表

関数 役割
the_custom_logo() ロゴを、出力する
get_custom_logo() ロゴのHTMLを、文字列で取得する
has_custom_logo() ロゴが、設定されているかを確認する

関連項目

has_custom_logo()、get_custom_logo()

公式リファレンス:the_custom_logo() | Function | WordPress Developer Resources

コメント

タイトルとURLをコピーしました