在 WordPress 5.9 中,添加了新的鉤子和函數(shù)來幫助開發(fā)人員處理文章、文章類型和分類法。
is_post_type_viewable?過濾器
WP 5.9 引入了新的is_post_type_viewable過濾器,允許開發(fā)人員掛鉤is_post_type_viewable()以覆蓋此函數(shù)執(zhí)行的檢查。
此過濾器公開$post_type對象以允許返回true或根據(jù)他們的需要返回false。預(yù)期的過濾值是一個布爾值。由于過濾的值可能會發(fā)生變化,包括數(shù)據(jù)類型,因此此提交包括is_bool()檢查,從而確保向后兼容并防止PHP?8.1+中的潛在類型錯誤。非布爾值(甚至 falsey 和 truthy 值)將導(dǎo)致函數(shù)返回false.
用法:
/**
* Override is_post_type_viewable() value for the "Books" custom post type.
*/
function wporg_books_is_not_a_viewable_post_type( $is_viewable, $post_type ) {
if ( __( 'Books', 'my-plugin' ) === $post_type->label ) {
return false;
}
return $is_viewable;
}
add_filter( 'is_post_type_viewable', 'wporg_books_is_not_a_viewable_post_type', 10, 2 );
Trac上的相關(guān)工單:#49628。
is_post_status_viewable?過濾器
與is_post_type_viewable 類似,is_post_status_viewable過濾器允許開發(fā)人員掛鉤到相關(guān)的 PHP 函數(shù)。此過濾器公開?$post_status?對象以允許返回true?或根據(jù)他們的需要返回?false。
用法:
/**
* Override is_post_status_viewable() value for the "Unread" custom post status.
*/
function wporg_unread_is_not_a_viewable_post_status( $is_viewable, $post_status ) {
if ( __( 'Unread', 'my-plugin' ) === $post_status->label ) {
return false;
}
return $is_viewable;
}
add_filter( 'is_post_type_viewable', 'wporg_unread_is_not_a_viewable_post_status', 10, 2 );
Trac 上的相關(guān)工單:#54375。
post_thumbnail_url?過濾器
WP 5.9 引入了post_thumbnail_url 允許覆蓋從wp_get_attachement_image_url()函數(shù)返回的默認(rèn) url 的新過濾器 。它傳遞以下參數(shù):
$thumbnail_url: 文章縮略圖URL(如果文章不存在,則為 false)$post:文章 ID 或 WP_Post 對象。默認(rèn)是全局的$post$size:用于檢索源或高度和寬度尺寸的平面數(shù)組的注冊圖像大小。默認(rèn)值:post-thumbnail
用法:
/**
* Override the post thumbnail URL for a specific template.
*/
function wporg_change_post_thumbnail_url_for_about_template( $thumbnail_url, $post, $size ) {
if ( 'templates/about.php' !== get_page_template_slug( $post ) ) {
return wp_get_attachment_image_url( get_template_directory . '/images/my-specific-image.png' );
}
return $thumbnail_url;
}
add_filter( 'post_thumbnail_url', 'wporg_change_post_thumbnail_url_for_about_template', 10, 3 );
Trac 上的相關(guān)工單:#40547。
post_thumbnail_id?過濾器
同樣,WP 5.9 引入了新的 post_thumbnail_id 過濾器,它允許覆蓋從 get_post_thumbnail_id(). 它傳遞以下參數(shù):
$thumbnail_id: 文章縮略圖 ID(如果文章不存在,則為 false)$post:文章 ID 或 WP_Post 對象。默認(rèn)是全局的$post
Trac 上的相關(guān)工單:#23983。
register_taxonomy() 新標(biāo)簽可用
在 WP 5.9 中,一些靜態(tài)字符串被替換為額外的標(biāo)簽選項,以允許開發(fā)人員更靈活地自定義Edit { taxonomy }屏幕。
添加了以下標(biāo)簽:
name_field_description:“編輯標(biāo)簽”屏幕上“名稱”字段的說明。默認(rèn)值:“名稱是它在您網(wǎng)站上的顯示方式。”parent_field_description:“編輯標(biāo)簽”屏幕上“父”字段的說明。默認(rèn)值:“分配父術(shù)語以創(chuàng)建層次結(jié)構(gòu)。例如,爵士樂這個詞將是 Bebop 和 Big Band 的父母。”slug_field_description:編輯標(biāo)簽屏幕上 Slug 字段的說明。默認(rèn)值:“? slug ? 是名稱的 URL 友好版本。它通常都是小寫的,只包含字母、數(shù)字和連字符。”desc_field_description:“編輯標(biāo)簽”屏幕上“說明”字段的說明。默認(rèn):“描述默認(rèn)不突出;但是,某些主題可能會顯示出來。”
Trac 上的相關(guān)工單:#43060。
用于獲取文章現(xiàn)有修訂版URL 的新函數(shù):wp_get_post_revisions_url()
從 WP 5.9 開始,該wp_get_post_revisions_url()函數(shù)可用于獲取給定文章修訂的鏈接。
參數(shù):
$post_id(可選):文章ID或WP_Post對象。默認(rèn)為 global?$post。
此函數(shù)返回用于在給定文章(或null其他方式)上編輯修訂的 URL?。
Trac 上的相關(guān)票證:#39062。
WP 5.9 中新的內(nèi)置文章類型
請注意,WordPress 5.9 引入了四種與新的完整站點(diǎn)編輯體驗相關(guān)的新內(nèi)置文章類型,并在激活塊主題時使用。
wp_template:要包含在您的主題中的模板。wp_template_part:要包含在模板中的模板部件。wp_global_styles:站點(diǎn)管理員為當(dāng)前主題創(chuàng)建和保存的樣式。wp_navigation:可以插入站點(diǎn)的導(dǎo)航菜單。
將發(fā)布其他開發(fā)說明以介紹新的完整站點(diǎn)編輯體驗。請注意,上述文章類型適用reserved terms于 WordPress 內(nèi)部使用。




