Puis-je définir une image sélectionnée par défaut pour une catégorie?
-
-
2 ansplustard,ilexiste unplugin WordPress quifait cela.Checkout "Imagesen vedettepour les catégories" https://wordpress.org/plugins/featured-images-for-categories/2 Years later, there is a WordPress plugin that does this. Checkout "Featured Images for Categories" https://wordpress.org/plugins/featured-images-for-categories/
-
Utilisez ceplugin: http://wordpress.org/extend/plugins/categories-images/Celafonctionnepour les catégorieset toutetaxonomiepersonnalisée aussi,vouspouvezen savoirplus sur leplugin,comment l'utiliseret beaucoup d'exemples dans les documentationsUse this plugin: http://wordpress.org/extend/plugins/categories-images/ It works for categories and any custom taxonomy too, you can find more about the plugin, how to use it and a lot of examples in the documentations
- 0
- 2015-03-28
- Muhammad
5 réponses
- votes
-
- 2010-12-07
Dans votremodèle où vous avez l'imageen vedette affichée:
<?php the_post_thumbnail( 'thumbnail' ); ?>
vouspouvez le conditionner si l'image sélectionnéeest définie,puis la définirpar défaut sur ce que vous voulez siellen'estpas définie.Unefaçon defaireest demettretoutes lesimagespar défaut dans un répertoireet de lesnommerpour les catégories,parexemple.news.jpget reviews.jpg alors au lieu d'utiliser
<?php the_post_thumbnail( 'thumbnail' ); ?>
pour afficher votreimage sélectionnée,vous utiliseriez ceci:<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) { the_post_thumbnail('thumbnail'); } else { ?> <img src="whatever/directory/<?php $category = get_the_category(); echo $category[0]->cat_name; ?>.jpg" /> <?php } endif; } ?>
Donc,dans cetexemple ci-dessus,si lemessageest dans la catégorie actualitéset que votre rédacteurn'apas défini l'imageen vedette,il serapar défaut sur l'image stockée à
http://www.yoursite/whatever/directory/news.jpg
.In your template where you have the Featured Image displayed:
<?php the_post_thumbnail( 'thumbnail' ); ?>
you can make it conditional on whether the Featured Image is set, then have it default to whatever you want if it's not set.One way to do this is to put all the default images in a directory and name them for the categories, eg. news.jpg and reviews.jpg then instead of using
<?php the_post_thumbnail( 'thumbnail' ); ?>
to display your Featured Image you'd use this:<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) { the_post_thumbnail('thumbnail'); } else { ?> <img src="whatever/directory/<?php $category = get_the_category(); echo $category[0]->cat_name; ?>.jpg" /> <?php } endif; } ?>
So in this example above if the post is in the news category and your writer didn't set the Featured Image it'll default to the image stored at
http://www.yoursite/whatever/directory/news.jpg
.-
C'est ainsi queje lefais aussi.+1This is how I do it as well. +1
- 0
- 2011-02-07
- epaps
-
que sepasse-t-il si un article appartient à quelques catégories?what happened if a post belongs to a few categories ?
- 0
- 2011-03-20
- Eray
-
Bonne question.`get_the_category ()`ne retournera qu'une seule catégorieet j'oublie commentil décide quelle catégorie utiliser.Good question. `get_the_category()` will only return one category and I forget how it decides which category to use.
- 0
- 2011-03-20
- matt
-
get_the category () obtienttoutes les catégories sousforme detableau,mais vous avezbesoin d'unepetite astuce.http://www.studiograsshopper.ch/code-snippets/category-description-with-multiple-categories-and-get_the_category/get_the category() is getting all categories as an array, but you need a liitle trick . http://www.studiograsshopper.ch/code-snippets/category-description-with-multiple-categories-and-get_the_category/
- 1
- 2011-03-23
- Eray
-
- 2011-07-13
Unemanièrepluspropre defaire ce quiprécède;)
if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) : the_post_thumbnail( 'thumbnail' ); else : ?><img src="whatever/directory/<?php $category = get_the_category(); echo $category[0]->cat_name; ?>.jpg" /><?php endif;
A cleaner way to do the above ;)
if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) : the_post_thumbnail( 'thumbnail' ); else : ?><img src="whatever/directory/<?php $category = get_the_category(); echo $category[0]->cat_name; ?>.jpg" /><?php endif;
-
- 2012-06-25
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail())) : ?> <?php the_post_thumbnail('thumbnail'); ?> <?php else :?> <img src="<?php bloginfo('template_directory'); ?>/your image directory name in theme folder/<?php $category = get_the_category(); echo $category[0]->cat_name; ?>.jpg" /> <?php endif;?>
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail())) : ?> <?php the_post_thumbnail('thumbnail'); ?> <?php else :?> <img src="<?php bloginfo('template_directory'); ?>/your image directory name in theme folder/<?php $category = get_the_category(); echo $category[0]->cat_name; ?>.jpg" /> <?php endif;?>
-
- 2012-07-13
J'ai rencontré desproblèmes avec lesnoms de catégories comportant desespaces,j'ai légèrementmodifié le code ci-dessuspour répondre àmesbesoins
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail())) : ?> <?php the_post_thumbnail('thumbnail'); ?> <?php else :?> <img src="whatever/directory/<?php $category = get_the_category(); echo $category[0]->cat_ID; ?>.jpg" /> <?php endif;?>
il suffit de changer
echo $category[0]->cat_name;
à
echo $category[0]->cat_ID;
qui rendra votreimage 16.jpg ou 3.jpgtout ce qui correspond à votrenuméro de catégorie.
I ran into some problems with category names having spaces i altered the above code a bit to fit my needs
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail())) : ?> <?php the_post_thumbnail('thumbnail'); ?> <?php else :?> <img src="whatever/directory/<?php $category = get_the_category(); echo $category[0]->cat_ID; ?>.jpg" /> <?php endif;?>
basically just change
echo $category[0]->cat_name;
to
echo $category[0]->cat_ID;
which will make your image 16.jpg or 3.jpg whatever corresponds with your category number.
-
Jepense quebeaucoup degenspréfèrent utiliser catid car c'estplus sûret vouspermet de changer le libellé de la catégorie ou le slug.I think many people prefer to use cat id as it's safer and allows you to change the category label or slug.
- 0
- 2012-07-13
- Damien
-
- 2012-10-01
J'ai utilisé avec succès les Images detaxonomie de Michael Fields - cela ajoute lagestionfonctions à lapage demodification de catégorie/taxonomie,de sorte que lesimagesprésentéesfonctionnentpour lestaxonomies commeelles lefont avec les articles/pages.Possèdetoutes lesfonctions dont vous avezbesoinpour votrethématisation.
I've successfully used Taxonomy Images by Michael Fields - it adds management functions to the category / taxonomy edit page, so that featured images work for taxonomies just as they do with posts/pages. Has all the functions you need for your theming.
Jemetsen place un site avec unthème quitireparti de lafonctionnalité "image sélectionnée" de WordPress.Étant donné quemes utilisateursfinauxne sontpas lesplustechnophiles aumonde,j'aimerais configurer les choses demanière à ce que les catégories aient une «image vedette»par défaut attribuée aumessage.Dans le cas oùplusieurs catégories seraient attribuées à un article,ilfaudrait simplement lapremière.
Existe-t-il unplugin ou unmoyen de coder ceci?