Des exemples d'ajout de champs personnalisés à l'éditeur de catégories?
1 réponses
- votes
-
- 2011-01-08
Non. Vous devez utiliser
wp_options
,car vousne pouvezpas créer denouveaux champs dans latable wp_term_taxonomy (si vous lefaites,dans laprochainemise àjour WP,vous lesperdrez).Donc:
// the option name define('MY_CATEGORY_FIELDS', 'my_category_fields_option'); // your fields (the form) add_filter('edit_category_form', 'my_category_fields'); function my_category_fields($tag) { $tag_extra_fields = get_option(MY_CATEGORY_FIELDS); ?> <table class="form-table"> <tr class="form-field"> <th scope="row" valign="top"><label for="_ce4-categoryTitle">Full Category Title</label></th> <td><input name="_ce4-categoryTitle" id="_ce4-categoryTitle" type="text" size="40" aria-required="false" value="<?php echo $tag_extra_fields[$tag->term_id]['my_title']; ?>" /> <p class="description">The title is optional but will be used in place of the name on the home page category index.</p></td> </tr> <tr class="form-field"> <th scope="row" valign="top"><label for="_ce4_fullDescription">Full Category Text for Landing Page</label></th> <td><textarea style="height:70px; width:100%;margin-left:-5px;" name="_ce4_fullDescription" id="_ce4_fullDescription"><?php echo $tag_extra_fields[$tag->term_id]['my_description']; ?></textarea> <p class="description">This text will appear on the category landing page when viewing all articles in a category. The image, you supply above, if any, will be used here and this content will wrap around it.</p></td> </tr> </table> <?php } // when the form gets submitted, and the category gets updated (in your case the option will get updated with the values of your custom fields above add_filter('edited_terms', 'update_my_category_fields'); function update_my_category_fields($term_id) { if($_POST['taxonomy'] == 'category'): $tag_extra_fields = get_option(MY_CATEGORY_FIELDS); $tag_extra_fields[$term_id]['my_title'] = strip_tags($_POST['_ce4-categoryTitle']); $tag_extra_fields[$term_id]['my_description'] = strip_tags($_POST['_ce4_fullDescription']); update_option(MY_CATEGORY_FIELDS, $tag_extra_fields); endif; } // when a category is removed add_filter('deleted_term_taxonomy', 'remove_my_category_fields'); function remove_my_category_fields($term_id) { if($_POST['taxonomy'] == 'category'): $tag_extra_fields = get_option(MY_CATEGORY_FIELDS); unset($tag_extra_fields[$term_id]); update_option(MY_CATEGORY_FIELDS, $tag_extra_fields); endif; }
Ilpeut y avoir des hooks liés aux catégoriespour les deux derniers,je ne les aipas recherchés. Je viens d'adapter le code ci-dessus àpartir d'un ancienthème dans lequel l'administrateurpeut attacher uneimage à unterme detaxonomiepersonnalisé ...
No. You have to use
wp_options
, because you can't create new fields in the wp_term_taxonomy table (If you do, in the next WP update you'll loose them).So:
// the option name define('MY_CATEGORY_FIELDS', 'my_category_fields_option'); // your fields (the form) add_filter('edit_category_form', 'my_category_fields'); function my_category_fields($tag) { $tag_extra_fields = get_option(MY_CATEGORY_FIELDS); ?> <table class="form-table"> <tr class="form-field"> <th scope="row" valign="top"><label for="_ce4-categoryTitle">Full Category Title</label></th> <td><input name="_ce4-categoryTitle" id="_ce4-categoryTitle" type="text" size="40" aria-required="false" value="<?php echo $tag_extra_fields[$tag->term_id]['my_title']; ?>" /> <p class="description">The title is optional but will be used in place of the name on the home page category index.</p></td> </tr> <tr class="form-field"> <th scope="row" valign="top"><label for="_ce4_fullDescription">Full Category Text for Landing Page</label></th> <td><textarea style="height:70px; width:100%;margin-left:-5px;" name="_ce4_fullDescription" id="_ce4_fullDescription"><?php echo $tag_extra_fields[$tag->term_id]['my_description']; ?></textarea> <p class="description">This text will appear on the category landing page when viewing all articles in a category. The image, you supply above, if any, will be used here and this content will wrap around it.</p></td> </tr> </table> <?php } // when the form gets submitted, and the category gets updated (in your case the option will get updated with the values of your custom fields above add_filter('edited_terms', 'update_my_category_fields'); function update_my_category_fields($term_id) { if($_POST['taxonomy'] == 'category'): $tag_extra_fields = get_option(MY_CATEGORY_FIELDS); $tag_extra_fields[$term_id]['my_title'] = strip_tags($_POST['_ce4-categoryTitle']); $tag_extra_fields[$term_id]['my_description'] = strip_tags($_POST['_ce4_fullDescription']); update_option(MY_CATEGORY_FIELDS, $tag_extra_fields); endif; } // when a category is removed add_filter('deleted_term_taxonomy', 'remove_my_category_fields'); function remove_my_category_fields($term_id) { if($_POST['taxonomy'] == 'category'): $tag_extra_fields = get_option(MY_CATEGORY_FIELDS); unset($tag_extra_fields[$term_id]); update_option(MY_CATEGORY_FIELDS, $tag_extra_fields); endif; }
There might be some category-related hooks for the last two, I didn't search for them. I just adapted the code above from a old theme of mine in which the admin can attach a image to a custom taxonomy term...
-
@AA: Merci,celafonctionnetrèsbien!Comment récupéreriez-vous la valeur dutitre lorsque vous visualisez lapage d'archive des catégories?@AA: Thanks, this works great! How would you retrieve the value for the title when you are viewing the category archive page?
- 0
- 2011-01-11
- Scott B
-
en utilisant `get_option (MY_CATEGORY_FIELDS)`using `get_option(MY_CATEGORY_FIELDS)`
- 0
- 2011-01-11
- onetrickpony
-
J'ai utilisé ce codeet celafonctionnetrèsbien.J'aitrouvé que le champ queje souhaite ajouterestplacé sous lebouton Ajouter une catégorie.Connaissez-vous unmoyen d'afficher lesnouveaux champs au-dessus dubouton?J'aiessayé de supprimer latable du codeet j'aijuste laissé letret letdmais celan'apasfonctionnéI used this code and it works great. I found that the field I want to add gets put below the add category button. Do you know a way to get the new fields to show above the button? I tried removing the table from the code and just left the tr and td but that didn't work
- 0
- 2012-09-25
- Jamie
-
oui,apparemment,WP amaintenant denouvelles actions que vouspouvez utiliser.Principalement `edit_category_form_fields` au lieu de`edit_category_form`.Voir [edit_tags.php] (http://core.svn.wordpress.org/trunk/wp-admin/edit-tags.php)yes, apparently WP has new actions now which you can use. Mainly `edit_category_form_fields` instead of `edit_category_form`. See [edit_tags.php](http://core.svn.wordpress.org/trunk/wp-admin/edit-tags.php)
- 0
- 2012-09-25
- onetrickpony
-
Utilisez add_action (
. "_ Add_form_fields",my_action)pour étendre l'éditeur deformulaire de catégorie.c'est-à-dire add_action ("store_add_form_fields",array ($this,'add_form_fields)) dans un contexte d'objetpour vos hooks d'actionpersonnaliséspour untype detaxonomie de' store '.La version 3.X + de WordPresspréfère leformat pour les hooks d'action,l'ancien style de "edit_category_form_fields"et d'autres sontmarqués comme "support hérité" dans le code,ce qui signifie qu'ilspeuvent/finirontpar disparaître.réf: http://core.trac.wordpress.org/browser/tags/3.4.2/wp-admin/edit-tags.php Use add_action(."_add_form_fields",my_action) to extend the category form editor. i.e. add_action("store_add_form_fields",array($this,'add_form_fields)) in an object context for your custom action hooks for a taxonomy type of 'store'. Version 3.X+ of WordPress prefers the format for action hooks, the older style of "edit_category_form_fields" and others are marked as "legacy support" in the code, which means they may/will eventually go away. ref: http://core.trac.wordpress.org/browser/tags/3.4.2/wp-admin/edit-tags.php - 0
- 2012-10-03
- Lance Cleveland
Jepense queje suis sur lepoint de casser cettenoix :)
J'essaye d'ajouter unensemble de champspersonnalisés à l'éditeur de catégories. Puisqueje nem'occupepas de laméta depublication,je crois quej'écriraimes valeurs de champ de catégoriepersonnalisée dans latable wp_term_taxonomyplutôt que dans latable wp_options. Est-ce correct?
S'ilexiste desexemples deprocédure à suivre,veuillezpartager un lien ou unbout de code. Jene saispas comment captureret enregistrermes champs de catégoriepersonnalisés.
Voicimon code ...