問題描述
我為我的主題啟用了 custom-logo,並將其與<?php the_custom_logo(); ?> 列印到標題中。有沒有機會直接新增更多的類到這個影像?預設情況下,它只附帶 custom-logo 。
最佳解決方案
WordPress 為自定義徽標定製提供了一個過濾鉤。掛鉤 get_custom_logo 是過濾器。要更改標誌類,此程式碼可能會幫助您。
add_filter( 'get_custom_logo', 'change_logo_class' );
function change_logo_class( $html ) {
$html = str_replace( 'custom-logo', 'your-custom-class', $html );
$html = str_replace( 'custom-logo-link', 'your-custom-class', $html );
return $html;
}
參考:How to change wordpress custom logo and logo link class
次佳解決方案
以下是一個建議,我們如何嘗試透過 wp_get_attachment_image_attributes 過濾器 (未測試) 新增類:
add_filter( 'wp_get_attachment_image_attributes', function( $attr )
{
if( isset( $attr['class'] ) && 'custom-logo' === $attr['class'] )
$attr['class'] = 'custom-logo foo-bar foo bar';
return $attr;
} );
您可以根據自己的需要調整課程。
第三種解決方案
當你發現自己的 the_custom_logo 依賴於 get_custom_logo,它本身呼叫 wp_get_attachment_image 新增 custom-logo 類。後一個函式有一個過濾器 wp_get_attachment_image_attributes,您可以使用它來處理影像屬性。
所以你可以做的是構建一個過濾器,檢查 custom-logo 類是否存在,如果是,則新增更多的類。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。