get_posts avec plusieurs catégories
6 réponses
- votes
-
- 2015-04-21
Commeindiqué dans le codex ,vouspouvez utiliser ce qui suit
$query = new WP_Query( array( 'category__and' => array( 2, 6 ) ) );
//l'article doit être dans la catégorie avec les ID 2 ET 6As documented in the codex you could use the following
$query = new WP_Query( array( 'category__and' => array( 2, 6 ) ) );
//post has to be in category with ID 2 AND 6 -
- 2015-04-21
Le Codex dit:
Remarque: Leparamètre de catégorie doit être l'ID de la catégorieet non lenom de la catégorie.
Remarque: Leparamètre categorypeut être une liste de catégories séparéespar des virgules,car lafonction
get_posts()
transmet leparamètre 'category' directement dansWP_Query
comme'cat'
.Ce serait donc lamême chose que de
WP_Query()
Paramètre de catégorie -cat
.The Codex says:
Note: The category parameter needs to be the ID of the category, and not the category name.
Note: The category parameter can be a comma separated list of categories, as the
get_posts()
function passes the 'category' parameter directly intoWP_Query
as'cat'
.So it'd be same as
WP_Query()
's Category parameter -cat
.-
Wow,j'aiprispour acquis que cela donnerait lemême résultat.Mafaute.Merci.Wow, i took for granted that this would give the same result. My bad. Thanks.
- 0
- 2015-04-21
- gubbfett
-
- 2015-04-21
Vouspouvez utiliser la classe WP_QUERYpour rechercher les articles dans certaines catégories spécifiques,voici unexemple:
$query = new WP_Query( 'cat=2,6,17,38' );
Voici un lien vers la documentation oùilsexpliquent comment utiliser les résultatspourcréer uneboucleet afficher les articles dans les résultats.
You can use the WP_QUERY class to search for the posts in some specific categories, here is an example:
$query = new WP_Query( 'cat=2,6,17,38' );
Here is a link to the documentation where they explain how to use the results to build a loop and display the posts in the results.
-
- 2016-03-13
Ilexiste unmoyenbeaucoupplus simple quin'implique que lafonction
get_posts()
.<?php $args1 = array( 'posts_per_page' => 6, 'offset'=> 0, 'category' => array(2,3,25,30)); $myposts1 = get_posts( $args1 ); foreach ( $myposts1 as $post ) : setup_postdata( $post ); ?> <div class="each-post col-lg-aynk col-md-aynk col-sm-aynk col-xs-aynk"> <a href="<?php the_permalink(); ?>"> <h2><?php the_title(); ?></h2> </a> <a href="<?php the_permalink(); ?>"> <div class="news-story-img"> <?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'secondary-image'); endif; ?> <?php the_post_thumbnail( get_permalink() ); ?> </div> </a> </div> <?php endforeach; wp_reset_postdata();?>
There is a much easier way that just involves the
get_posts()
function.<?php $args1 = array( 'posts_per_page' => 6, 'offset'=> 0, 'category' => array(2,3,25,30)); $myposts1 = get_posts( $args1 ); foreach ( $myposts1 as $post ) : setup_postdata( $post ); ?> <div class="each-post col-lg-aynk col-md-aynk col-sm-aynk col-xs-aynk"> <a href="<?php the_permalink(); ?>"> <h2><?php the_title(); ?></h2> </a> <a href="<?php the_permalink(); ?>"> <div class="news-story-img"> <?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'secondary-image'); endif; ?> <?php the_post_thumbnail( get_permalink() ); ?> </div> </a> </div> <?php endforeach; wp_reset_postdata();?>
-
Veuillezexpliquer **pourquoi ** cela aide.Please explain **why** this helps.
- 2
- 2016-03-13
- kaiser
-
Jene peuxpas voterpour la suppression,mais le codene correspondmêmepas au commentaire concernant `get_posts ()`.I can't vote to delete, but the code doesn't even match the comment regarding `get_posts()`.
- 0
- 2016-12-29
- Alexander Holsgrove
-
-
- 2017-07-21
Si vous souhaitez utiliser nom_catégorie commeparamètre de recherche,vouspouvez utiliser
WP_Query( array("category_name" => "cat_1+cat_2+...+cat_n") )
,oùcat_1, cat_2, ... , cat_n
sont toutes les catégories que lemessage doit avoirpour être renvoyé àpartir de la requêteDocumentationici Codex Wordpress
If you want to use category_name as search parameter, you can use
WP_Query( array("category_name" => "cat_1+cat_2+...+cat_n") )
, wherecat_1, cat_2, ... , cat_n
are all the categories that the post must have to be returned from the queryDocumentation here Codex Wordpress
Sije fais unget_posts () avec une catégorie définie sur 1,2,j'obtiendraitous lesmessages de la catégorie 1 OU 2. Sije veux quetous les articles aient les deux catégories,c'est-à-dire 1 ET 2,commentpuis-jefaire cette demande?