问题描述
我有一个为一个目录创建的自定义帖子类型,最终将按字母顺序排序。我将按标题按字母顺序排列帖子,所以我想确保标题以姓氏/名字输入。有没有办法更改默认的帮助文本 – 「在这里输入标题」 – 在我的自定义帖子到别的什么?
最佳解决方案
没有办法明确地定制该字符串。但是它通过翻译功能,因此易于过滤。
尝试这样的东西 (不要忘了改变你的帖子类型):
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 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。