在 WordPress 编辑器中为自定义文章类型设置默认内容

在《WordPress TinyMCE 编辑器增强技巧大全》中,倡萌已经提到过为 WordPress TinyMCE编辑器设置默认文章内容的方法,今天主要是补充一下如果为自定义文章类型设置默认内容。什么是自定义文章类型

wpdaxue.com-201303471

为默认的文章类型(post)设置默认内容

回顾一下,WordPress 默认的文章类型为 post,我们可以在 functions.php 中添加下面的代码,就可以了:

add_filter( 'default_content', 'my_editor_content' );
 
function my_editor_content( $content ) {
 
	$content = "欢迎给 WordPress大学 投稿";
 
	return $content;
}

自定义文章类型(products)设置默认内容

那如果你有一种自定义文章类型为 product ,那你可以使用下面的代码,注意看第三行的 if 判断语句:

add_filter( 'default_content', 'default_products_content' );
 
function default_products_content( $content ) {
 
    if( $_GET['post_type'] == 'products' ) $content = "预设文字内容";
 
    return $content;
 
}

为多种自定义文字类型设置默认内容

上面举得例子是只给一种文章类型设置默认内容,如果你有很多种自定义文章类型,你可以使用下面的代码,更加简洁:

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;
}

上面的代码一共给几种文章类型(sources、stories、pictures 和 默认)设置默认内容,最后的“default:“表示除了上面的 3 种,其他的文章类型都是使用它的默认内容。

参考资料:http://justintadlock.com/archives/2009/04/05/how-to-preset-text-in-the-wordpress-post-editor

来源:

https://www.wpdaxue.com/default-content-for-custom-post-types.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32480.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?