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

如果你每篇文章或页面都需要插入同一个自定义字段和值,可以考虑在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);
}
}

注:根据自己的需要,修改第 10 行的字段名和字段值。也可以修改 5、6 行决定是发布文章还是页面才添加。

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2023 Tadke.com. 琼ICP备20000547号