將下面的代碼添加到當前主題的 functions.php 文件:
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, 'field-name', 'custom value', true);
}
}
只需替換其中的自定義欄目名稱:field-name,值:custom value
之後,當你發佈文章後,相應的自定義欄目名稱及值會自動添加到文章中。