Afficher l'image d'une catégorie à l'aide de get_categories, ou afficher une image de n'importe quel article enfant
-
-
Vous devez d'abord décider laquelle de ces deux choses vous voulez.Ensuite,vouspouvez ajoutern'importe quel code que vouspossédez déjà.First you should decide which of those two things you want. Then you could add any code you might already have.
- 2
- 2014-02-19
- kraftner
-
Jepréfèreprendre uneimageen vedette àpartir d'un article,mais si cette solutionest lourdeet que l'autreest simple,je veux la solution laplus simpleId prefer to take a featured image from a post but if that solution is unwieldy and the other is simple then id want the simpler solution
- 0
- 2014-02-20
- Jon
-
Btw,si vous cherchez unplugin,il yen a un appelé [Category Thumbnails] (http://wordpress.org/plugins/category-thumbnails/) quipourraitfaire l'affairepour vous.Jene l'aipastestémoi-même,mais l'utilisationest assezexplicite -et décrite dans le référentiel deplugins.Btw, if you're looking for a plugin, there is one called [Category Thumbnails](http://wordpress.org/plugins/category-thumbnails/) that might do the trick for you. Haven't tested it myself, but the usage is pretty much self-explanatory - and described at the plugin repository.
- 0
- 2014-02-25
- Nicolai
-
maisj'auraisbesoin de l'appeler dans unget_categories ou une autrefonction similaire,et cettepartien'estpas si clairebut I would need to call it in a get_categories or other such function, and that part is not so clear
- 0
- 2014-02-25
- Jon
-
Vous devez décider lequel des deux vous voulez,sinon cen'estpas une question,mais 3 questions.Commentfaire X,Commentfaire Yet,Dois-je utiliser X ou YYou should decide which of the two you want, else this is not a question, but 3 questions. How do I do X, How do I do Y, and, Should I use X or Y
- 0
- 2014-03-01
- Tom J Nowell
-
Je soupçonne également que vous comprenezmal commentfonctionnent lesmessageset lestermes hiérarchiques,unterme contient automatiquementtous lespostes attribués auxtermesenfantset petits-enfants.Parexemple.Toutes les chansons du sous-genre dancepopfonttoujourspartie dugenre Popprincipal,et ilen va demêmepour lestermes hiérarchiques dans WordPress.Cettepartie compterait également comme une quatrième questionI also suspect you misunderstand how posts and hierarchical terms work, a term contains all the posts assigned to child and grandchild terms automatically. E.g. All songs in the dance pop sub-genre are still a part of the main Pop genre, and the same is true of hierarchical terms in WordPress. This part would also count as a 4th question
- 0
- 2014-03-01
- Tom J Nowell
-
Jepense que vousmanquez laforêt des arbres.La questionest de savoir commentest-ce que A. X & Y semblent être lesmoyens de lefaire.En utilisant leplus simple de X ou Y,commentpuis-jefaire A. Faire une relectureI think you are missing the forest from the trees. The question is how do I do A. X & Y seem like the ways to do this. Using the simpler of either X or Y how do I do A. Have a re-read
- 0
- 2014-03-01
- Jon
-
3 réponses
- votes
-
- 2014-03-01
Ceciestpossible avec unfiltre sur
get_terms
.function grab_child_image($terms,$taxonomies,$args) { // var_dump($terms,$taxonomies,$args); // debug foreach ($terms as &$term) { $cp = new WP_Query( array ( 'cat' => $term->term_id, 'fields' => 'ids', 'ignore_sticky_posts' => true ) ); // var_dump($cp->posts); // debug if ($cp->have_posts()) { $attach = new WP_Query( array ( 'post_parent__in' => $cp->posts, 'post_type' => 'attachment', 'post_status' => 'inherit', 'ignore_sticky_posts' => true, 'posts_per_page' => 1 ) ); if ($attach->have_posts()) { $term->image = wp_get_attachment_image($attach->posts[0]->ID); } else { $term->image = 'some other image'; } } } return $terms; } add_filter('get_terms','grab_child_image',10,3); $args = array('child_of' => 1 ); $categories = get_categories($args); foreach($categories as $category) { echo '<p>Category:'. $category->name.' </p> '; echo '<p> Description:'. $category->description . '</p>'; echo $category->image; } remove_filter('get_terms','grab_child_image',10,3);
Il y aplusieurs requêtes là-dedans,donc ajoutez cefiltre uniquement lorsque vousen avezbesoinet supprimez lefiltrepar la suite.
Ilexiste un certainnombre defonctions d'image associées que vouspouvez utiliser à laplace,si
wp_get_attachment_image()
ne fonctionnepaspour vosbesoins,et vouspouvezpasser unparamètre$size
àwp_get_attachment_image()
- deuxièmeparamètre--pour obtenir différentestailles d'image. Parexemple,remplacez la ligne de codepar ceci:$term->image = wp_get_attachment_image($attach->posts[0]->ID, $size->full);
Vouspouvezmodifier davantage la sortie de
wp_get_attachment_image()
en appliquant unfiltre àwp_get_attachment_image_attributes
-parexemple,pour ajouter une classe commefaitici .This is possible with a filter on
get_terms
.function grab_child_image($terms,$taxonomies,$args) { // var_dump($terms,$taxonomies,$args); // debug foreach ($terms as &$term) { $cp = new WP_Query( array ( 'cat' => $term->term_id, 'fields' => 'ids', 'ignore_sticky_posts' => true ) ); // var_dump($cp->posts); // debug if ($cp->have_posts()) { $attach = new WP_Query( array ( 'post_parent__in' => $cp->posts, 'post_type' => 'attachment', 'post_status' => 'inherit', 'ignore_sticky_posts' => true, 'posts_per_page' => 1 ) ); if ($attach->have_posts()) { $term->image = wp_get_attachment_image($attach->posts[0]->ID); } else { $term->image = 'some other image'; } } } return $terms; } add_filter('get_terms','grab_child_image',10,3); $args = array('child_of' => 1 ); $categories = get_categories($args); foreach($categories as $category) { echo '<p>Category:'. $category->name.' </p> '; echo '<p> Description:'. $category->description . '</p>'; echo $category->image; } remove_filter('get_terms','grab_child_image',10,3);
There are several queries in there, so add that filter only when you need it and remove the filter afterwards.
There are a number of related image functions that you could use instead, if
wp_get_attachment_image()
doesn't work for your needs, and you can pass a$size
parameter towp_get_attachment_image()
-- second parameter-- to get different image sizes. For example replace the line of code with this:$term->image = wp_get_attachment_image($attach->posts[0]->ID, $size->full);
You can further alter the output of
wp_get_attachment_image()
by applying a filter towp_get_attachment_image_attributes
-- for example, to add a class as done here.-
fonctionne -excellent!2 questions.Sesminiaturestirantes.Comment changer celapour l'image sélectionnée,pas la versionminiature?Aussi comment ajouter une classe à labaliseimg.Merciworks - excellent! 2 Questions. Its pulling thumbnails. How would I change this to the featured image, not thumbnail version? Also how would I add a class to the img tag. Thanks
- 0
- 2014-03-01
- Jon
-
@Jon: voir lamodification.@Jon : see the edit.
- 0
- 2014-03-01
- s_ha_dum
-
J'ai ajouté le code detravail réelpour le changement detaille carje ne savaispas quoifaire avant d'essayer quelques choses.I added the actual working code for the size change as I wasnt sure what to do till I tried a few things.
- 0
- 2014-03-02
- Jon
-
Changement de classe - Lors de laboucle,vous devez retirer lefiltre de suppression de lafonctionet leplacer après laboucle,ou celane fonctionne qu'une seulefois.Re changing class - When looping you need to take the remove filter out of the function, and put it after the loop, or it only works once.
- 0
- 2014-03-02
- Jon
-
`remove_filter ()` _is_ après laboucle `foreach` dansmon code.`remove_filter()` _is_ after the `foreach` loop in my code.
- 0
- 2014-03-02
- s_ha_dum
-
désolé,je faisais référence à remove_filter ('wp_get_attachment_image_attributes','alter_attr_wpse_102158');àpartir de votre réponse liéepour ajouter une classe.J'aimis cette suppression avec la suppressionget_terms.Mais votre réponse liée abesoin d'unpeu debricolage,si quelqu'un d'autre lit ceciet restebloqué.sorry I was referring to the remove_filter('wp_get_attachment_image_attributes','alter_attr_wpse_102158'); from your linked answer for adding class. I put that remove with the remove get_terms. But your linked answer needs a bit of tinkering, if anyone else is reading this and stuck.
- 0
- 2014-03-02
- Jon
-
- 2014-02-25
Ehbien,j'ai réussi àfaire ce queje pense que vous voulezfaireen utilisant Champspersonnalisés avancés ,en créant unnouvelensemble de champspersonnalisés avec un champpersonnalisé image . Àpartir de là,définissez les règles de localisation sur "Terme detaxonomie"> "est égal à"> "Catégories" (ou quelle que soit lataxonomiepour laquelle vous l'utilisiez).
Unefois que vouspubliez ceci,il devraitmettre àjour où vous éditez lestaxonomies avec unnouveau champpour uneimage. Maintenant,toutest question d'utiliser lafonction ACF "get_field ()" dans votreboucle categoryforeach. Voici comment obtenir un champ depuis la catégorie.
Exemple:
<?php foreach ( $categories as $category ) { echo get_field('field_name_for_image', $category->taxonomy . "_" . $category->term_id); } ?>
Si vous avez utilisé lesparamètrespar défautpour ACF,vous devriezen extraire untableau,quiest l’objetimage,et pour cela,lire sur Types de champs d'image d'ACF.
Ilm'afallu & lt; 5minutespour configurer cela,et lepluginestgratuit. J'espère que c'est rapideet facilepour vous!
Bonne chance!
Well, I managed to do what I believe you want to do using Advanced Custom Fields, creating a new set of custom fields with one image custom field. From there, set the rules on location to "Taxonomy Term" > "is equal to" > "Categories" (or whatever taxonomy you were using this for).
Once you publish this, it should update where you edit the taxonomies with a new field for an image. Now, it's all a matter of using the ACF function "get_field()" inside your category foreach loop. Here's how you get field from the category.
Example:
<?php foreach ( $categories as $category ) { echo get_field('field_name_for_image', $category->taxonomy . "_" . $category->term_id); } ?>
If you used the default settings for ACF, you should get an array out of this, which is the image object, and for that, read up on Image Field Types from ACF.
It took me <5 minutes to set this up, and the plugin is free. I hope that's quick and easy for you!
Best of luck!
-
Je vaisjeter un oeilet voir sije peux lefairefonctionner.MerciIll take a look and see if I can make it work. Thanks
- 0
- 2014-02-25
- Jon
-
- 2014-02-26
Wordpress a le code - http://codex.wordpress.org/Function_Reference/get_the_category
Show Category Images This outputs category images named after the cat_ID with the alt attribute set to cat_name. You can also use any of the other member variables instead. <?php foreach((get_the_category()) as $category) { echo '<img src="http://example.com/images/' . $category->cat_ID . '.jpg" alt="' . $category->cat_name . '" />'; } ?>
Celapermet une configuration d'images dans unefonctionget_categories.Lesimages doivent être configuréespour correspondre à lafonction.Donc,si $ category->nameest utilisé,l'image doit êtreenregistrée dans lebon dossier avec lenom correspondant de la catégorie.
Wordpress has the code - http://codex.wordpress.org/Function_Reference/get_the_category
Show Category Images This outputs category images named after the cat_ID with the alt attribute set to cat_name. You can also use any of the other member variables instead. <?php foreach((get_the_category()) as $category) { echo '<img src="http://example.com/images/' . $category->cat_ID . '.jpg" alt="' . $category->cat_name . '" />'; } ?>
This allows a set up of images inside a get_categories functions. The images have to be setup to match the function. So if $category->name is used, the image must be saved in the right folder with the matching name of the category.
J'utiliseget_categoriespour répertorier les catégoriesenfants d'une catégorieparent.
Je souhaite ajouter uneimage aux catégoriesenfants,en utilisant la sortieget_categories.
Le code quej'utilise actuellementest
foreach($categories as $category)
.Mais quelleest lameilleure (et laplus simple)façon demettreen œuvre cela?