欢迎光临
我们一直在努力

WordPress发布文章/页面时自动添加默认的自定义字段

建站超值云服务器,限时71元/月

如果你每篇文章或页面都需要插入同一个自定义字段和值,可以考虑在WordPress发布文章/页面时,自动添加默认的自定义字段。将下面的代码添加到当前主题的 functions.php 即可:

1
2
3
4
5
6
7
8
9
10
11
12
/**
 * WordPress发布文章/页面时自动添加默认的自定义字段
 * https://www.wpdaxue.com/add-custom-field-automatically-post-page-publish.html
 */
add_action('publish_page', 'add_custom_field_automatically');//发布页面时
add_action('publish_post', 'add_custom_field_automatically');//发布文章时
function add_custom_field_automatically($post_ID) {
	global $wpdb;
	if(!wp_is_post_revision($post_ID)) {
		add_post_meta($post_ID, '字段名', '字段值', true);
	}
}

/** * WordPress发布文章/页面时自动添加默认的自定义字段 * https://www.wpdaxue.com/add-custom-field-automatically-post-page-publish.html */ add_action(‘publish_page’, ‘add_custom_field_automatically’);//发布页面时 add_action(‘publish_post’, ‘add_custom_field_automatically’);//发布文章时 function add_custom_field_automatically($post_ID) { global $wpdb; if(!wp_is_post_revision($post_ID)) { add_post_meta($post_ID, ‘字段名’, ‘字段值’, true); } }

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » WordPress发布文章/页面时自动添加默认的自定义字段
分享到: 更多 (0)