向WordPress二十主題添加其他默認標題


“二十十”主題帶有八個默認標題。 您設置的任何默認標題都將替換為帖子的“特色圖片”。 您只能在查看帖子時看到新標題。 在本週的WordPress快速提示中,我們將看到在標題面板中添加其他標題是多麼簡單。

觀看截屏

Functions.php

打開“二十十”主題的functions.php文件。 找到以下部分:

// Default custom headers packaged with the theme. %s is a placeholder for the theme template directory URI.

接下來是一個列出當前默認標題及其位置的數組。 要添加自己的內容,請在日落標頭數組的最後一個括號後面添加一個逗號。 這是添加兩個標頭後的示例:

register_default_headers( array(
‘berries’ => array(
‘url’ => ‘%s/images/headers/berries.jpg’,
‘thumbnail_url’ => ‘%s/images/headers/berries-thumbnail.jpg’,
/* translators: header image description */
‘description’ => __( ‘Berries’, ‘twentyten’ )
),
‘cherryblossom’ => array(
‘url’ => ‘%s/images/headers/cherryblossoms.jpg’,
‘thumbnail_url’ => ‘%s/images/headers/cherryblossoms-thumbnail.jpg’,
/* translators: header image description */
‘description’ => __( ‘Cherry Blossoms’, ‘twentyten’ )
),
‘concave’ => array(
‘url’ => ‘%s/images/headers/concave.jpg’,
‘thumbnail_url’ => ‘%s/images/headers/concave-thumbnail.jpg’,
/* translators: header image description */
‘description’ => __( ‘Concave’, ‘twentyten’ )
),
‘fern’ => array(
‘url’ => ‘%s/images/headers/fern.jpg’,
‘thumbnail_url’ => ‘%s/images/headers/fern-thumbnail.jpg’,
/* translators: header image description */
‘description’ => __( ‘Fern’, ‘twentyten’ )
),
‘forestfloor’ => array(
‘url’ => ‘%s/images/headers/forestfloor.jpg’,
‘thumbnail_url’ => ‘%s/images/headers/forestfloor-thumbnail.jpg’,
/* translators: header image description */
‘description’ => __( ‘Forest Floor’, ‘twentyten’ )
),
‘inkwell’ => array(
‘url’ => ‘%s/images/headers/inkwell.jpg’,
‘thumbnail_url’ => ‘%s/images/headers/inkwell-thumbnail.jpg’,
/* translators: header image description */
‘description’ => __( ‘Inkwell’, ‘twentyten’ )
),
‘path’ => array(
‘url’ => ‘%s/images/headers/path.jpg’,
‘thumbnail_url’ => ‘%s/images/headers/path-thumbnail.jpg’,
/* translators: header image description */
‘description’ => __( ‘Path’, ‘twentyten’ )
),
‘sunset’ => array(
‘url’ => ‘%s/images/headers/sunset.jpg’,
‘thumbnail_url’ => ‘%s/images/headers/sunset-thumbnail.jpg’,
/* translators: header image description */
‘description’ => __( ‘Sunset’, ‘twentyten’ )
),
‘waterfall’ => array(
‘url’ => ‘%s/images/headers/waterfall.jpg’,
‘thumbnail_url’ => ‘%s/images/headers/waterfall-thumbnail.jpg’,
/* translators: header image description */
‘description’ => __( ‘Waterfall’, ‘twentyten’ )
),
‘mountain’ => array(
‘url’ => ‘%s/images/headers/mountain.jpg’,
‘thumbnail_url’ => ‘%s/images/headers/mountain-thumbnail.jpg’,
/* translators: header image description */
‘description’ => __( ‘Mountain’, ‘twentyten’ )
)
) );

讓我們剖析一下代碼。

register_default_headers: 這是創建和顯示默認標題的函數。 它接受參數數組。

名稱: 用標題名稱設置一個值。 然後,我們可以設置其他值和鍵的數組。

網址: 標題圖片的相對路徑。 請注意,當前路徑為/images/headers/image_name.jpg

thumbnail_url: 標題圖片縮略圖的相對路徑。 當前路徑是/images/headers/image_name-thumbnail.jpg

描述: 圖片說明。 將鼠標懸停在“標題”面板內的圖像上時,將顯示該內容。

注意事項

“二十十”標頭的尺寸為940×198,大於該尺寸的任何尺寸都將被調整; 任何較小的物體都會拉伸以適合這些尺寸。 縮略圖可以設置為任意尺寸,默認為230×48。 圖片只能是.jpg,否則將被忽略並且不會顯示。