Trier les résultats par nom et ordre ascendant sur Archive.php
-
-
si vous utilisez une requêtepersonnalisée dans votre archive.php,pouvez-vous l'afficher?peut êtreposter le archive.php complet sur http://pastie.orget mettre àjour votre réponse avec le lien?if you are using custom query in your archive.php can you please show it? may be post the complete archive.php on http://pastie.org and update your answer with the link?
- 0
- 2012-01-23
- Hameedullah Khan
-
3 réponses
- votes
-
- 2012-01-23
Lemoyen leplus simple de lefaireest d'utiliser un hook (le hook
pre_get_posts
)pour changer l'ordre.Mais vous devez vérifier que la requêteen est unepour laquelle vous souhaitezmodifier l'ordre!(is_archive()
ouis_post_type_archive()
devrait être suffisant.)Parexemple,mettez ce qui suit dans lefunctions.php de votrethème ...
add_action( 'pre_get_posts', 'my_change_sort_order'); function my_change_sort_order($query){ if(is_archive()): //If you wanted it for the archive of a custom post type use: is_post_type_archive( $post_type ) //Set the order ASC or DESC $query->set( 'order', 'ASC' ); //Set the orderby $query->set( 'orderby', 'title' ); endif; };
The easiest way to do this is to use a hook (the
pre_get_posts
hook) to change the order. But you should check that the query is one for which you do want to alter the order! (is_archive()
oris_post_type_archive()
should be sufficient.)For instance, put the following in your theme's functions.php...
add_action( 'pre_get_posts', 'my_change_sort_order'); function my_change_sort_order($query){ if(is_archive()): //If you wanted it for the archive of a custom post type use: is_post_type_archive( $post_type ) //Set the order ASC or DESC $query->set( 'order', 'ASC' ); //Set the orderby $query->set( 'orderby', 'title' ); endif; };
-
Salut,pourrez-vousmontrer commentfonctionne la commandepar défaut?Quelques liens sipossible .MerciHi, will you be able to show how does the default ordering works? Some links if possible .thanks
- 0
- 2019-09-07
- Latheesh V M Villa
-
@LatheeshVMVilla WPest développéen tant queblog,donc le classementpar défaut/par défaut sefaitparpost_date DESC (=décroissant),donc c'est leplus récent après lepremier.Si vous utilisez WPpour quelque chose où letempsestmoinspertinent (laplupart destypes de listes,comme votre collection d'enregistrements,vos recettes,unglossaire,...),vous voudrez souvent commanderpost_title ASC (=Croissant,donc alphabétiquepartitreavec des chiffresprécédant les lettres).@LatheeshVMVilla WP is developed as a blog, so the sensible/default ordering is by post_date DESC (=descending), so that's newest-post-first. If you're using WP for something where time is less relevant (most types of lists, like your records collection, recipes, a glossary, ...) you'll want to order often post_title ASC (=Ascending, so alphabetic by title with numbers preceding letters).
- 0
- 2019-12-03
- user3445853
-
Merci.Fonctionnebien pourmapage d'archive detaxonomie.Thanks. Works fine for my taxonomy archive page.
- 0
- 2020-02-05
- Sema Hernández
-
Aparfaitementfonctionné.Je vous remercie!Worked perfectly. Thank you!
- 0
- 2020-06-17
- Mark P
-
- 2017-01-20
<?php // we add this, to show all posts in our // Glossary sorted alphabetically if ( is_category('Glossary') ) { $args = array( 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC' ); $glossaryposts = get_posts( $args ); } foreach( $glossaryposts as $post ) : setup_postdata( $post ); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?>
<?php // we add this, to show all posts in our // Glossary sorted alphabetically if ( is_category('Glossary') ) { $args = array( 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC' ); $glossaryposts = get_posts( $args ); } foreach( $glossaryposts as $post ) : setup_postdata( $post ); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?>
-
Pouvez-vousexpliquer _pourquoi_ cela aide le PO?Veuilleztoujours ajouter uneexplication au-dessus d'unmorceau de code.Merci.Can you please explain _why_ this helps the OP? Please, always add an explanation on top of a piece of code. Thanks.
- 1
- 2017-01-20
- kaiser
-
La question était Trier les résultatsparnomet ordre ascendant sur Archive.php.Vraisemblablement selon le répondant,ce codetriera les résultatsparnomet ordre ascendant sur Archive.php?The question was Sort results by name & asc order on Archive.php. Presumably according to the answerer this code will Sort results by name & asc order on Archive.php?
- 0
- 2019-12-11
- Jon
-
- 2012-01-23
Suite à la réponse de Stephen,si vous souhaitez simplementinterrogeret classerpartitre,vouspouvez l'utiliser dans votrefichiermodèle:
$args = ( array( 'order' => 'ASC', 'orderby' => 'title', ) ); query_posts($args);
further to Stephen's answer, if you want to just query and order by the title, you could use this in your template file:
$args = ( array( 'order' => 'ASC', 'orderby' => 'title', ) ); query_posts($args);
-
Directement àpartir de la référence de code WordPress - "Cettefonction remplacera complètement la requêteprincipaleet n'estpas destinée à être utiliséepar desplugins ou desthèmes. Son approchetrop simpliste de lamodification de la requêteprincipalepeut êtreproblématiqueet doit être évitée dans laplupart des cas.cas,ilexiste demeilleures optionsplusperformantespourmodifier la requêteprincipale,parexemple via l'action 'pre_get_posts' dans WP_Query. " Enbout de ligne,@Stephen Harris a labonnefaçon d'accomplir cela. https://developer.wordpress.org/reference/functions/query_posts/Straight from the WordPress code reference - "This function will completely override the main query and isn’t intended for use by plugins or themes. Its overly-simplistic approach to modifying the main query can be problematic and should be avoided wherever possible. In most cases, there are better, more performant options for modifying the main query such as via the ‘pre_get_posts’ action within WP_Query." Bottom line @Stephen Harris has the correct way of accomplishing this. https://developer.wordpress.org/reference/functions/query_posts/
- 5
- 2016-10-13
- Michael
J'utilise actuellement le code suivantpour lister les articles dans Archive.phpmaisje veux que les résultats soient classésparnom dans l'ordre croissant,j'ai vérifié le codexmais la réponsen'estpas clairepourmoi,commentpuis-je l'obtenirtravaille?
Merci d'avance.