如果每篇文章或页面都需要插入同一个自定义字段和值,可以考虑在WordPress发布文章/页面时,自动添加默认的自定义字段。
- 使用钩子 publish_post
- 使用判断 wp_is_post_revision($post_ID)
- 使用方法 add_post_meta
// add_action('publish_page', 'add_custom_field_automatically');//发布页面时
// add_action('publish_post', 'add_custom_field_automatically');//发布文章时
add_action('save_post', 'set_post_clicknum');// 添加文章时
function set_post_clicknum($post_ID) {
if(!wp_is_post_revision($post_ID)) { // 判断文章是否是修改版
add_post_meta($post_ID, '字段名', '字段值', true);
}
}