我正在尝试修改代码,以便根据帖子类型显示默认内容,但到目前为止,我没有成功。基本代码为:
add_filter( \'default_content\', \'my_editor_content\' );
function my_editor_content( $content ) {
$content = "default content goes here....";
return $content;
}
我的修改包括:
add_filter( \'default_content\', \'my_editor_content\' );
function my_editor_content( $content ) {
if ( \'sources\' == get_post_type() ) {
$content = "Please insert an image of the document into this area. If there is no image, please descript the document in detail.";
return $content;
} elseif ( \'stories\' == get_post_type() ) {
$content = "Please write your reminiscences, recollections, memories, anecdotes, and remembrances in this area.";
return $content;
} elseif ( \'pictures\' == get_post_type() ) {
$content = "Please insert an image of a photograph into this area.";
return $content;
} else {
$content = "default!";
return $content;
};}
但这根本不起作用。我觉得我错过了显而易见的事情。
最合适的回答,由SO网友:t31os 整理而成
使用第二个参数$post
和检查$post->post_type
除了交换机之外,它比其他几种if-else-if-else等更易于使用。。
add_filter( \'default_content\', \'my_editor_content\', 10, 2 );
function my_editor_content( $content, $post ) {
switch( $post->post_type ) {
case \'sources\':
$content = \'your content\';
break;
case \'stories\':
$content = \'your content\';
break;
case \'pictures\':
$content = \'your content\';
break;
default:
$content = \'your default content\';
break;
}
return $content;
}
希望这有帮助。。