自定义栏目是WordPress 一个非常强大的功能,借助它我们可以实现很多特殊的功能。本文的一段代码可以为文章自动添加预先设置好的自定义栏目名称及值,而无需手动添加。
WordPress自定义栏目
将下面的代码添加到当前主题的 functions.php 文件:
- add_action(\’publish_page\’, \’add_custom_field_automatically\’);
- add_action(\’publish_post\’, \’add_custom_field_automatically\’);
- add_custom_field_automatically() {
- ;
- (!wp_is_post_revision()) {
- add_post_meta(, \’field-name\’, \’custom value\’, true);
- }
- }
你只需替换其中的自定义栏目名称:field-name,值:custom value
之后,当你发布文章后,相应的自定义栏目名称及值会自动添加到文章中。
代码源自
暂无评论内容