Ajouter une vignette à wp_list_categories ()
1 réponses
- votes
-
- 2014-12-08
La question d'origine utilise
get_tax_meta ()
quin'existepas dans le cœur de WordPressmaispeut être unefonctionpersonnalisée crééepar le demandeur. Dans ce cas,nouspourrions également le désactiver avecget_term_meta ()
.Vouspouvezessayer ceci:
La classeList_Category_Images étend Walker_Category { function start_el (& amp; $ sortie,$ category,$ depth=0,$ args=array (),$id=0) { $ Saved_data=get_tax_meta ($ category- >term_id,'image_field_id',true); $ cat_name=apply_filters ( 'list_cats', esc_attr ($ category- >nom), $ catégorie ); $ link='& lt; a href="'.esc_url (get_term_link ($ category)). '"'; if ($ args ['use_desc_for_title'] & amp; & amp;! vide ($ category- > description)) { $ link.='title="'.esc_attr (strip_tags (apply_filters ('category_description',$ category- > description,$ category))). '"'; } $ lien.='>'; $ link.='& lt;img src="'. $ save_data ['src']. '" >'; $ link.=$nom_cat. '& lt;/a >'; if (! vide ($ args ['show_count'])) { $ link.='('.number_format_i18n ($ category- > count). ')'; } if ('list'==$ args ['style']) { $ output.="\t & lt; li"; $ class='objet-chat-objet-chat-'. $ category- >term_id; if (! vide ($ args ['current_category'])) { $ _current_category=get_term ($ args ['current_category'],$ category- >taxonomie); if ($ category- >term_id==$ args ['current_category']) { $ class.='chat-actuel'; }elseif ($ category- >term_id==$ _current_category- >parent) { $ class.='chat-actuel-parent'; } } $ output.='class="'. $ class. '"'; $ output.="> $ lien \n"; } autre { $ output.="\t $ link & lt;br/> \n"; } } }
Ce queje fais,c'estmodifier l'une desméthodes de la classe Walker_Category qui dans ce cas,lafonction créeen fait le lien vers leterme/la catégorie. En haut de laméthode,j'appelle lafonction
get_tax_meta ()
(qui,je suppose,fonctionne,carellen'estpasintégrée à WordPress). Ensuite,j'ajoute l'image directement avant lenom de la catégorie:$ link.='>'; $ link.='& lt;img src="'. $ saved_data ['src']. '" >'; $ link.=$nom_cat. '& lt;/a >';
Ilne vous resteplus qu'à définir unenouvelleinstance de cette classe dans votrefonction
wp_list_categories ()
:$ args=tableau ( 'orderby'=> 'Nom', 'show_count'=> 0, 'pad_counts'=> 0, 'hierarchical'=> 1, 'taxonomie'=> $taxe, 'walker'=>nouveau List_Category_Images ); wp_list_categories ($ args);
The original question uses
get_tax_meta()
which does not exist in WordPress core but may be a custom function created by the asker. In this case we could switch it out just as well withget_term_meta()
.You could try this:
class List_Category_Images extends Walker_Category { function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) { $saved_data = get_tax_meta( $category->term_id, 'image_field_id', true ); $cat_name = apply_filters( 'list_cats', esc_attr( $category->name ), $category ); $link = '<a href="' . esc_url( get_term_link( $category ) ) . '" '; if ( $args['use_desc_for_title'] && ! empty( $category->description ) ) { $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"'; } $link .= '>'; $link .= '<img src="' . $saved_data['src'] . '">'; $link .= $cat_name . '</a>'; if ( ! empty( $args['show_count'] ) ) { $link .= ' (' . number_format_i18n( $category->count ) . ')'; } if ( 'list' == $args['style'] ) { $output .= "\t<li"; $class = 'cat-item cat-item-' . $category->term_id; if ( ! empty( $args['current_category'] ) ) { $_current_category = get_term( $args['current_category'], $category->taxonomy ); if ( $category->term_id == $args['current_category'] ) { $class .= ' current-cat'; } elseif ( $category->term_id == $_current_category->parent ) { $class .= ' current-cat-parent'; } } $output .= ' class="' . $class . '"'; $output .= ">$link\n"; } else { $output .= "\t$link<br />\n"; } } }
What I'm doing is modifying one of the methods inside the Walker_Category class which in this case the function is actually creating the link to the term / category. At the top of the method I call the
get_tax_meta()
function ( which I assume works, as this is not built into WordPress ). Then I add the image directly before the name of the category:$link .= '>'; $link .= '<img src="' . $saved_data['src'] . '">'; $link .= $cat_name . '</a>';
Now all you need to do is define a new instance of this class in your
wp_list_categories()
function:$args = array( 'orderby' => 'name', 'show_count' => 0, 'pad_counts' => 0, 'hierarchical' => 1, 'taxonomy' => $tax, 'walker' => new List_Category_Images ); wp_list_categories( $args );
-
Comme la source de `get_tax_meta`estinconnue,alors remplacer` $ saved_data`par `$image=get_field ('thumb',$ category);` à la ligne 3 (etparimplication à la ligne 13) où 'thumb'est le champpersonnalisénom,renvoyé sousforme d'URL,fait l'affaireAs the source of `get_tax_meta` is unknown, then replacing `$saved_data` with `$image = get_field('thumb', $category);` on line 3 (and by implication line 13) where 'thumb' is the custom field name, returned as URL, does the trick
- 0
- 2019-09-18
- Chris Pink
-
Notez que `get_field ()`n'est disponible que dans leplugin Advanced Custom Fields.J'ai utilisé `get_tax_meta ()` comme ce qu'OP a utilisé dans leur question.Dans d'autresextraits,cela devrait être remplacépar `get_term_meta ()`Note that `get_field()` is only available in the Advanced Custom Fields plugin. I used `get_tax_meta()` as that what OP used in their question. In other snippets this should be replaced with `get_term_meta()`
- 0
- 2019-09-18
- Howdy_McGee
J'utilise le code suivantpour afficher une liste demestermes detaxonomie surmapage d'accueil:
J'essaiemaintenant d'ajouter autitre,letermeminiature quej'avais configuré avec un champpersonnalisé dans cette liste (queje peux afficher avec le code suivant):
Jene sais vraimentpas comment lesmélangerensemble ... J'ai lu sur la classe demarcheurpersonnalisémaisje ne saispas comment l'utiliser. Quelqu'unpeut-ilme montrer unexemple de lamanière d'yparvenir?