説明
has_custom_logo() は、カスタムロゴが設定されているかを判定する関数です。
基本構文
has_custom_logo( int $blog_id = 0 ): bool
引数
$blog_id:サイトID
戻り値
設定されていれば true。
使い方(サンプル)
if ( has_custom_logo() ) {
the_custom_logo();
}
注意点
custom-logoのテーマサポートが必要です。
実務での使いどころ
- ロゴが設定されているときだけ、ロゴの枠を出すとき。
- 未設定のときに、サイト名の文字などへ、切り替えるとき。
よくあるミスと対処
- テーマが、
custom-logoをサポートしていない場合、設定ができず、常にfalse。 - 出力ではなく、判定のための関数。ロゴの出力には、
the_custom_logo()。
NG例
if ( has_custom_logo() ) { // custom-logo をサポートしていないと、常に false
// ...
}
OK例
add_action( 'after_setup_theme', function () {
add_theme_support( 'custom-logo' );
} );
使い分け早見表
| 関数 | 役割 |
|---|---|
has_custom_logo() |
ロゴが、設定されているかを確認する |
the_custom_logo() |
ロゴを、出力する |
get_custom_logo() |
ロゴのHTMLを、文字列で取得する |
関連項目
the_custom_logo()、get_custom_logo()
公式リファレンス:has_custom_logo() | Function | WordPress Developer Resources

コメント