Add a custom field to every public post type in WordPress by pasting this to your functions.php. Make sure you have Advanced Custom Fields Pro installed and activated. The custom fields will appear as a Option Page in WordPress admin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
add_action( 'init', 'add_option_page_with_taxonomies' ); function add_option_page_with_taxonomies(){ if(function_exists('acf_add_options_page')){ acf_add_options_page(array( 'page_title' => 'Arkivinställningar för posttyper', 'menu_title' => 'Arkivinställningar', 'menu_slug' => 'archive-settings', 'capability' => 'edit_posts', 'icon_url' => 'dashicons-clipboard', 'redirect' => false )); $custom_post_types = get_post_types( array('public' => true) ); foreach($custom_post_types as $custom_post_type){ $obj = get_post_type_object( $custom_post_type ); //echo $obj->labels->singular_name; if( function_exists('acf_add_local_field_group') ){ acf_add_local_field_group(array ( 'key' => 'group_' . $custom_post_type, 'title' => 'Inställningar för ' . $custom_post_type, 'fields' => array ( array ( 'key' => 'key_ingress_for_' . $custom_post_type, 'label' => 'Ingress för ' . $custom_post_type, 'name' => 'ingress_for_' . $custom_post_type, 'type' => 'text', 'prefix' => '', 'instructions' => '', 'required' => 0, 'conditional_logic' => 0, 'wrapper' => array ( 'width' => '', 'class' => '', 'id' => '', ), 'default_value' => '', 'placeholder' => '', 'prepend' => '', 'append' => '', 'maxlength' => '', 'readonly' => 0, 'disabled' => 0, ) ), 'location' => array ( array ( array ( 'param' => 'options_page', 'operator' => '==', 'value' => 'archive-settings', ), ), ), 'menu_order' => 0, 'position' => 'normal', 'style' => 'seamless', 'label_placement' => 'top', 'instruction_placement' => 'label', 'hide_on_screen' => '', )); } } } } |
Print the custom field on the archive page by adding this to the template file.
1 2 3 |
$post_type = get_post_type(); $ingress = get_field('key_ingress_for_' . $post_type, 'option'); echo '<h2>' . $ingress . '</h2>'; |