問題描述
我有一個為一個目錄創建的自定義帖子類型,最終將按字母順序排序。我將按標題按字母順序排列帖子,所以我想確保標題以姓氏/名字輸入。有沒有辦法更改默認的幫助文本 – 「在這裏輸入標題」 – 在我的自定義帖子到別的什麼?
最佳解決方案
沒有辦法明確地定製該字符串。但是它通過翻譯功能,因此易於過濾。
嘗試這樣的東西 (不要忘了改變你的帖子類型):
add_filter('gettext','custom_enter_title');
function custom_enter_title( $input ) {
global $post_type;
if( is_admin() && 'Enter title here' == $input && 'your_post_type' == $post_type )
return 'Enter Last Name, Followed by First Name';
return $input;
}
次佳解決方案
我知道我在這裏的晚會有點晚了,但我想補充説,在 WordPress v3.1 中,專門為此添加了 enter_title_here 過濾器。
add_filter( 'enter_title_here', 'custom_enter_title' );
function custom_enter_title( $input ) {
if ( 'your_post_type' === get_post_type() ) {
return __( 'Enter your name here', 'your_textdomain' );
}
return $input;
}
更改 your_post_type 和 your_textdomain 以匹配您自己的帖子類型名稱和文本域。
第三種解決方案
對不起,從墳墓挖掘這個問題,但 WordPress 3.1 提供了更好的解決方案。 enter_title_here 過濾器。
function change_default_title( $title ){
$screen = get_current_screen();
// For CPT 1
if ( 'custom_post_type_1' == $screen->post_type ) {
$title = 'CPT1 New Title';
// For CPT 2
} elseif ( 'custom_post_type_2' == $screen->post_type ) {
$title = 'CPT2 New Title';
// For Yet Another CPT
} elseif ( 'custom_post_type_3' == $screen->post_type ) {
$title = 'CPT3 New Title';
}
// And, so on
return $title;
}
add_filter( 'enter_title_here', 'change_default_title' );
第四種方案
看看 wp-admin/edit-form-advanced.php 第 246 行 (第 329 行,從 WP3.5 開始)
<label class="screen-reader-text" id="title-prompt-text" for="title">
<?php echo apply_filters( 'enter_title_here', __( 'Enter title here' ), $post ); ?>
</label>
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。