Campi personalizzati in tassonomia personalizzata – woocommerce
-
Buongiorno a tutti.
Ho creato una tassonomia personalizzata che ho chiamato provincia tramite il seguente codice inserito in functions.php tema storefront.function add_custom_taxonomies() { // Add new "Provincia" taxonomy to Posts register_taxonomy('provincia', 'product', array( // Hierarchical taxonomy (like categories) 'hierarchical' => true, // This array of options controls the labels displayed in the WordPress Admin UI 'labels' => array( 'name' => _x( 'provincia', 'taxonomy general name' ), 'singular_name' => _x( 'Provincia', 'taxonomy singular name' ), 'search_items' => __( 'Search Provincia' ), 'all_items' => __( 'All Provincia' ), 'parent_item' => __( 'Parent Provincia' ), 'parent_item_colon' => __( 'Parent Provincia:' ), 'edit_item' => __( 'Edit Provincia' ), 'update_item' => __( 'Update Provincia' ), 'add_new_item' => __( 'Add New Provincia' ), 'new_item_name' => __( 'New Provincia Name' ), 'menu_name' => __( 'Provincia' ), ), // Control the slugs used for this taxonomy 'rewrite' => array( 'slug' => 'provincia', // This controls the base slug that will display before each term 'with_front' => false, // Don't display the category base before "/locations/" 'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/" ), )); } add_action( 'init', 'add_custom_taxonomies', 0 );
Poi ho aggiunto un campo personalizzato dentro il vocabolario appena creato con questa funzione.
function wcr_provincia_fields($term) { // we check the name of the action because we need to have different output // if you have other taxonomy name, replace category with the name of your taxonomy. ex: book_add_form_fields, book_edit_form_fields if (current_filter() == 'provincia_edit_form_fields') { $short_description = get_term_meta($term->term_id, 'short_description', true); $color_code = get_term_meta($term->term_id, 'color_code', true); ?> <tr class="form-field"> <th valign="top" scope="row"><label for="term_fields[short_description]"><?php _e('Short description'); ?></label></th> <td> <textarea class="large-text" cols="50" rows="5" id="term_fields[short_description]" name="term_fields[short_description]"><?php echo esc_textarea($short_description); ?></textarea><br/> <span class="description"><?php _e('Please enter short description'); ?></span> </td> </tr> <tr class="form-field"> <th valign="top" scope="row"><label for="term_fields[color_code]"><?php _e('Color code'); ?></label></th> <td> <input type="text" size="40" value="<?php echo esc_attr($color_code); ?>" id="term_fields[color_code]" name="term_fields[color_code]"><br/> <span class="description"><?php _e('Please enter color hex code'); ?></span> </td> </tr> <?php } elseif (current_filter() == 'provincia_add_form_fields') { ?> <div class="form-field"> <label for="term_fields[short_description]"><?php _e('Short description'); ?></label> <textarea cols="40" rows="5" id="term_fields[short_description]" name="term_fields[short_description]"></textarea> <p class="description"><?php _e('Please enter short description'); ?></p> </div> <div class="form-field"> <label for="term_fields[color_code]"><?php _e('Color code'); ?></label> <input type="text" size="40" value="" id="term_fields[color_code]" name="term_fields[color_code]"> <p class="description"><?php _e('Please enter color hex code'); ?></p> </div> <?php } } // Add the fields, using our callback function // if you have other taxonomy name, replace provincia with the name of your taxonomy. ex: book_add_form_fields, book_edit_form_fields add_action('provincia_add_form_fields', 'wcr_provincia_fields', 10, 2); add_action('provincia_edit_form_fields', 'wcr_provincia_fields', 10, 2); // salva i dati function wcr_save_provincia_fields($term_id) { if (!isset($_POST['term_fields'])) { return; } foreach ($_POST['term_fields'] as $key => $value) { update_term_meta($term_id, $key, sanitize_text_field($value)); } } // Save the fields values, using our callback function // if you have other taxonomy name, replace category with the name of your taxonomy. ex: edited_book, create_book add_action('edited_provincia', 'wcr_save_provincia_fields', 10, 2); add_action('create_provincia', 'wcr_save_provincia_fields', 10, 2);
e tutto funziona bene. Il campo short_description si vede in edit del termina e si salva correttamente
ho creato la cartella woocommercedentro il tema e creato il file taxonomy-provincia.php per personalizzare la visualizzazione dei prodotti associa ti al vocabolario.Ora, cio, premesso, come faccio a visualizzare il campo short_description a mezzo del file sovrascritto taxonomy-provincia.php?
Grazie Francesco
Stai visualizzando 6 risposte - dal 1 al 6 (di 6 totali)
Stai visualizzando 6 risposte - dal 1 al 6 (di 6 totali)
- Il topic ‘Campi personalizzati in tassonomia personalizzata – woocommerce’ è chiuso a nuove risposte.