I try to add some default custom fields only to the pages in wordpress with this code at my functions.php:
// Adding Custom Fields
add_action('save_post', 'set_default_custom_fields', '99');
function set_default_custom_fields($post_id){
if ( $_GET['post_type'] == 'page' ) {
add_post_meta($post_id, 'Header', '', true);
add_post_meta($post_id, 'Überschrift', '', true);
}
}
The custom fields are showing by adding a new page but not if I EDIT a existing page. If a page is still there and "published" it will not be showing. Whats wrong with my code?
You can also use Advanced Custom Fields. It's a free plugin with adds custom fields to any post_type, so also to pages.
http://advancedcustomfields.com It can also be implemented into your php code. It works really great!
Good luck!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.