{"id":18819,"date":"2018-06-19T15:06:03","date_gmt":"2018-06-19T13:06:03","guid":{"rendered":"https:\/\/it.wordpress.org\/support\/topic\/campi-personalizzati-in-tassonomia-personalizzata-woocommerce\/"},"modified":"2018-06-19T19:37:53","modified_gmt":"2018-06-19T17:37:53","slug":"campi-personalizzati-in-tassonomia-personalizzata-woocommerce","status":"publish","type":"topic","link":"https:\/\/it.wordpress.org\/support\/topic\/campi-personalizzati-in-tassonomia-personalizzata-woocommerce\/","title":{"rendered":"Campi personalizzati in tassonomia personalizzata &#8211; woocommerce"},"content":{"rendered":"<p>Buongiorno a tutti.<br \/>\nHo creato una tassonomia personalizzata che ho chiamato provincia tramite il seguente codice inserito in functions.php tema storefront.<\/p>\n<pre><code>\nfunction add_custom_taxonomies() {\n  \/\/ Add new &quot;Provincia&quot; taxonomy to Posts\n  register_taxonomy(&#039;provincia&#039;, &#039;product&#039;, array(\n    \/\/ Hierarchical taxonomy (like categories)\n    &#039;hierarchical&#039; =&gt; true,\n    \/\/ This array of options controls the labels displayed in the WordPress Admin UI\n    &#039;labels&#039; =&gt; array(\n      &#039;name&#039; =&gt; _x( &#039;provincia&#039;, &#039;taxonomy general name&#039; ),\n      &#039;singular_name&#039; =&gt; _x( &#039;Provincia&#039;, &#039;taxonomy singular name&#039; ),\n      &#039;search_items&#039; =&gt;  __( &#039;Search Provincia&#039; ),\n      &#039;all_items&#039; =&gt; __( &#039;All Provincia&#039; ),\n      &#039;parent_item&#039; =&gt; __( &#039;Parent Provincia&#039; ),\n      &#039;parent_item_colon&#039; =&gt; __( &#039;Parent Provincia:&#039; ),\n      &#039;edit_item&#039; =&gt; __( &#039;Edit Provincia&#039; ),\n      &#039;update_item&#039; =&gt; __( &#039;Update Provincia&#039; ),\n      &#039;add_new_item&#039; =&gt; __( &#039;Add New Provincia&#039; ),\n      &#039;new_item_name&#039; =&gt; __( &#039;New Provincia Name&#039; ),\n      &#039;menu_name&#039; =&gt; __( &#039;Provincia&#039; ),\n    ),\n    \/\/ Control the slugs used for this taxonomy\n    &#039;rewrite&#039; =&gt; array(\n      &#039;slug&#039; =&gt; &#039;provincia&#039;, \/\/ This controls the base slug that will display before each term\n      &#039;with_front&#039; =&gt; false, \/\/ Don&#039;t display the category base before &quot;\/locations\/&quot;\n      &#039;hierarchical&#039; =&gt; true \/\/ This will allow URL&#039;s like &quot;\/locations\/boston\/cambridge\/&quot;\n    ),\n  ));\n}\nadd_action( &#039;init&#039;, &#039;add_custom_taxonomies&#039;, 0 );\n<\/code><\/pre>\n<p>Poi ho aggiunto un campo personalizzato dentro il vocabolario appena creato con questa funzione.<\/p>\n<pre><code>\nfunction wcr_provincia_fields($term) {\n    \/\/ we check the name of the action because we need to have different output\n    \/\/ if you have other taxonomy name, replace category with the name of your taxonomy. ex: book_add_form_fields, book_edit_form_fields\n    if (current_filter() == &#039;provincia_edit_form_fields&#039;) {\n        $short_description = get_term_meta($term-&gt;term_id, &#039;short_description&#039;, true);\n        $color_code = get_term_meta($term-&gt;term_id, &#039;color_code&#039;, true);\n        ?&gt;\n        &lt;tr class=&quot;form-field&quot;&gt;\n            &lt;th valign=&quot;top&quot; scope=&quot;row&quot;&gt;&lt;label for=&quot;term_fields[short_description]&quot;&gt;&lt;?php _e(&#039;Short description&#039;); ?&gt;&lt;\/label&gt;&lt;\/th&gt;\n            &lt;td&gt;\n                &lt;textarea class=&quot;large-text&quot; cols=&quot;50&quot; rows=&quot;5&quot; id=&quot;term_fields[short_description]&quot; name=&quot;term_fields[short_description]&quot;&gt;&lt;?php echo esc_textarea($short_description); ?&gt;&lt;\/textarea&gt;&lt;br\/&gt;\n                &lt;span class=&quot;description&quot;&gt;&lt;?php _e(&#039;Please enter short description&#039;); ?&gt;&lt;\/span&gt;\n            &lt;\/td&gt;\n        &lt;\/tr&gt;\n        &lt;tr class=&quot;form-field&quot;&gt;\n            &lt;th valign=&quot;top&quot; scope=&quot;row&quot;&gt;&lt;label for=&quot;term_fields[color_code]&quot;&gt;&lt;?php _e(&#039;Color code&#039;); ?&gt;&lt;\/label&gt;&lt;\/th&gt;\n            &lt;td&gt;\n                &lt;input type=&quot;text&quot; size=&quot;40&quot; value=&quot;&lt;?php echo esc_attr($color_code); ?&gt;&quot; id=&quot;term_fields[color_code]&quot; name=&quot;term_fields[color_code]&quot;&gt;&lt;br\/&gt;\n                &lt;span class=&quot;description&quot;&gt;&lt;?php _e(&#039;Please enter color hex code&#039;); ?&gt;&lt;\/span&gt;\n            &lt;\/td&gt;\n        &lt;\/tr&gt;   \n\t&lt;?php } elseif (current_filter() == &#039;provincia_add_form_fields&#039;) {\n        ?&gt;\n        &lt;div class=&quot;form-field&quot;&gt;\n            &lt;label for=&quot;term_fields[short_description]&quot;&gt;&lt;?php _e(&#039;Short description&#039;); ?&gt;&lt;\/label&gt;\n            &lt;textarea cols=&quot;40&quot; rows=&quot;5&quot; id=&quot;term_fields[short_description]&quot; name=&quot;term_fields[short_description]&quot;&gt;&lt;\/textarea&gt;\n            &lt;p class=&quot;description&quot;&gt;&lt;?php _e(&#039;Please enter short description&#039;); ?&gt;&lt;\/p&gt;\n        &lt;\/div&gt;\n        &lt;div class=&quot;form-field&quot;&gt;\n            &lt;label for=&quot;term_fields[color_code]&quot;&gt;&lt;?php _e(&#039;Color code&#039;); ?&gt;&lt;\/label&gt;\n            &lt;input type=&quot;text&quot; size=&quot;40&quot; value=&quot;&quot; id=&quot;term_fields[color_code]&quot; name=&quot;term_fields[color_code]&quot;&gt;\n            &lt;p class=&quot;description&quot;&gt;&lt;?php _e(&#039;Please enter color hex code&#039;); ?&gt;&lt;\/p&gt;\n        &lt;\/div&gt;  \n    &lt;?php\n    }\n}\n\n\/\/ Add the fields, using our callback function  \n\/\/ if you have other taxonomy name, replace provincia with the name of your taxonomy. ex: book_add_form_fields, book_edit_form_fields\nadd_action(&#039;provincia_add_form_fields&#039;, &#039;wcr_provincia_fields&#039;, 10, 2);\nadd_action(&#039;provincia_edit_form_fields&#039;, &#039;wcr_provincia_fields&#039;, 10, 2);\n   \n \n \n\/\/ salva i dati\n \n \nfunction wcr_save_provincia_fields($term_id) {\n    if (!isset($_POST[&#039;term_fields&#039;])) {\n        return;\n    }\n\n    foreach ($_POST[&#039;term_fields&#039;] as $key =&gt; $value) {\n        update_term_meta($term_id, $key, sanitize_text_field($value));\n    }\n}\n\n\/\/ Save the fields values, using our callback function\n\/\/ if you have other taxonomy name, replace category with the name of your taxonomy. ex: edited_book, create_book\nadd_action(&#039;edited_provincia&#039;, &#039;wcr_save_provincia_fields&#039;, 10, 2);\nadd_action(&#039;create_provincia&#039;, &#039;wcr_save_provincia_fields&#039;, 10, 2);\n<\/code><\/pre>\n<p>e tutto funziona bene. Il campo short_description si vede in edit del termina e si salva correttamente<br \/>\nho creato la cartella woocommercedentro il tema e creato il file taxonomy-provincia.php per personalizzare la visualizzazione dei prodotti associa ti al vocabolario.<\/p>\n<p>Ora, cio, premesso, come faccio a visualizzare il campo short_description a mezzo del file sovrascritto taxonomy-provincia.php?<\/p>\n<p>Grazie Francesco<\/p>\n","protected":false},"template":"","class_list":["post-18819","topic","type-topic","status-publish","hentry","topic-tag-campo-personalizzato","topic-tag-tassonomia-personalizzata"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/it.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/18819","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/it.wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/it.wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":1,"href":"https:\/\/it.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/18819\/revisions"}],"predecessor-version":[{"id":18830,"href":"https:\/\/it.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/18819\/revisions\/18830"}],"wp:attachment":[{"href":"https:\/\/it.wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=18819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}