Comment utiliser the_posts_navigation pour wp_query et get_posts?
-
-
Votre questionesttrompeuse,puisque votre code utilise `the_posts_pagination`,c'est différent avec votre question detitre`the_posts_navigation`.Your question is mislead, since your code use `the_posts_pagination`, it's different with your title question `the_posts_navigation`.
- 1
- 2016-04-02
- Jevuska
-
3 réponses
- votes
-
- 2015-01-15
the_posts_navigation()
est simplement unefonction wrapperpourget_the_posts_navigation()
quiest simplement unefonction wrapperpourpaginate_links
. Les deuxpremièresfonctions utilisent lesmêmesparamètresexacts que ceux utilisésparpaginate_links
et lestransmettent également à cette dernièrefonctionget_the_posts_navigation()
etthe_posts_navigation()
sont debonnesnouvellesfonctions carelles éliminentbeaucoup de codagepersonnaliséet sontplus convivialespour lesnouveaux utilisateursinexpérimentés qui souhaitentnuméroter liens depaginationLe seul défaut dans cette
get_the_posts_navigation()
est que les développeurs sont allésencapsuler lafonctionpaginate_links
dans uneinstruction conditionnelle quiindique que si la requêteprincipale ($wp_query
) amoins d'unepage,(rappelez-vous,lapremièrepageest0
et la deuxièmepageest2
),ne pas afficher les liens. Ceciestproblématiquepour les requêtespersonnalisées sur lesmodèles depage. Lespages auronttoujours une seulepage,donc cesfonctionsne fonctionnentpas avec les requêtespersonnaliséesLa seule vraie solution de contournement si vous devez utiliser
the_posts_navigation()
,est d'utiliser la réponse @ChipBennet dans cemessage . Jen'aime vraimentpas annuler la requêteprincipale (assez hacky,àmon avis,c'est comme utiliserquery_posts
)maisje ne vois aucune autre solutionpourfaireget_the_posts_navigation()
pourtravailler avec des requêtespersonnaliséesthe_posts_navigation()
is simply a wrapper function forget_the_posts_navigation()
which issimply a wrapper function forpaginate_links
. The first two functions uses the the same exact parameters that is being used bypaginate_links
and actually passes it to the latter function as wellget_the_posts_navigation()
andthe_posts_navigation()
is good new functions as it eliminates a lot of custom coding and it is more user friendly for new unexperienced users who would like numbered pagination linksThe only flaw in this
get_the_posts_navigation()
is that the developers went and wrapped thepaginate_links
function in a conditional statement that states that if the main query ($wp_query
) has less than 1 page, (remember, the first page is0
and the second page is2
), don't show the links. This is problematic for custom queries on page templates. Pages will always have just one page, so these functions does not work with custom queriesThe only true workaround if you have to use
the_posts_navigation()
, is to make use of @ChipBennet answer in this post. I really don't like nullifying the main query (quite hacky, in my opinion this is just like usingquery_posts
) but I can't see any other solution to makeget_the_posts_navigation()
to work with custom queries-
Mercipour votre réponse.Maintenant,je sais que lafonctionne peutpasfonctionner avec des requêtespersonnalisées.Aufait,vousn'aimezpas annuler la requêteprincipale.Mais quelleméthode avez-vous utilisée?Thank you for your reply. Now I know the function can't to work with custom queries. By the way, you don't like nullifying the main query. But which method did you used?
- 0
- 2015-01-16
- Vincent Wong
-
Jen'utilisepas lafonctionnative,j'ai écritmaproprefonction depagination quej'utilise.Vouspouvez le vérifier [ici] (http://wordpress.stackexchange.com/a/172818/31545)I don't make use of the native function, I wrote my own pagination function which I use. You can check it out [here](http://wordpress.stackexchange.com/a/172818/31545)
- 0
- 2015-01-16
- Pieter Goosen
-
Sij'utilise unpermalien comme celui-ci http://127.0.0.1/gdboer/?post_type=case celan'apasfonctionné.utiliser le lienpermanent http://127.0.0.1/gdboer/?page_id=74est OK.If I use permalink like this http://127.0.0.1/gdboer/?post_type=case it didn't work. use permalink http://127.0.0.1/gdboer/?page_id=74 is OK.
- 0
- 2015-01-17
- Vincent Wong
-
Je vaisjouer avec ça la semaineprochaine.Pris dans quelques autres choses à ce stade.Ungrandmercipour votre réponse.Celam'aide vraimentbeaucoup.I will play around with this in the coming week. Caught up in a few other things at this stage. Many thanks for your reply. This really helps me a lot.
- 0
- 2015-01-17
- Pieter Goosen
-
- 2015-04-25
J'ai unmodèlepersonnaliséet j'aieu dumal à afficher le composant depagination. voici ce qui afonctionnépourmoi.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( 'posts_per_page' => 3, 'orderby' => 'menu_order', 'order'=> 'ASC', 'paged'=>$paged, 'post_type' => 'projects' ); $projects = new WP_Query($args); <!-- working example of pagination with numbers --> ...<?php endwhile;?> <?php $GLOBALS['wp_query']->max_num_pages = $projects->max_num_pages; the_posts_pagination( array( 'mid_size' => 1, 'prev_text' => __( 'Back', 'green' ), 'next_text' => __( 'Onward', 'green' ), 'screen_reader_text' => __( 'Posts navigation' ) ) ); ?> OR <!-- working example of pagination without numbers --> ...<?php endwhile;?> <?php next_posts_link( 'next', $projects->max_num_pages ); ?> <?php previous_posts_link('prev') ?>
I have a custom template and I struggled hours to show the pagination component. here what's worked for me.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( 'posts_per_page' => 3, 'orderby' => 'menu_order', 'order'=> 'ASC', 'paged'=>$paged, 'post_type' => 'projects' ); $projects = new WP_Query($args); <!-- working example of pagination with numbers --> ...<?php endwhile;?> <?php $GLOBALS['wp_query']->max_num_pages = $projects->max_num_pages; the_posts_pagination( array( 'mid_size' => 1, 'prev_text' => __( 'Back', 'green' ), 'next_text' => __( 'Onward', 'green' ), 'screen_reader_text' => __( 'Posts navigation' ) ) ); ?> OR <!-- working example of pagination without numbers --> ...<?php endwhile;?> <?php next_posts_link( 'next', $projects->max_num_pages ); ?> <?php previous_posts_link('prev') ?>
-
Certainement uneidée,la seulepartie queje n'aimepas vraimentest de changer l'objet de requêteprincipal.C'est une sorte depiratageet unbug qui va vous donner des cauchemars car vousne réinitialisezpas la requêteprincipale unefois que vous avezterminé.Gardez cela à l'esprit.C'estpourquoi lamondialisationest une chose simauvaise quetout lemondepeut changer les valeurs.N'oubliezpas que denombreuxplugins reposent sur desglobaux,en particulier `$ wp_query`et` $post`Definitely an idea, the only part that I don't really like is changing the main query object. It is kind of hacky and a bug that is going to give you nightmares because you do not reset the main query once you are done. Just keep that in mind. That is why globalisation is such an evil thing, anyone can change the values. Remember, many plugins rely on globals, specially `$wp_query` and `$post`
- 1
- 2015-04-25
- Pieter Goosen
-
Mercibeaucoup.`$ GLOBALS ['wp_query'] ->max_num_pages` était lebit manquantpourmoi.Jene suispas sûr qu'ilexiste unmeilleurmoyen.Many thanks. `$GLOBALS['wp_query']->max_num_pages` was the missing bit for me. Not sure there is a better way.
- 1
- 2017-08-10
- Ben
-
- 2015-01-15
Cettefonction utilise
get_the_posts_pagination()
quiutilise lafonction GLOBALwp_query
pour configurer lafonctionpaginate_links()
,doncje pense que celane fonctionnepaspourget_posts
.Essayez d'utiliser lafonction
paginate_links()
seule ou lafonctionposts_nav_link()
PS: assurez-vous d'utiliser
wp_reset_query()
This function uses the
get_the_posts_pagination()
which uses the GLOBALwp_query
to setup thepaginate_links()
function, so I believe that doesn't work forget_posts
.Try use the function
paginate_links()
by itself or the functionposts_nav_link()
PS: Make sure you use
wp_reset_query()
-
Mercipour votre réponse.Je sais quepaginate_linksest untravailpourmoi.maisje veux utiliser lanouvellefonctionthe_posts_navigation.J'ai regardé attentivement le code source deget_the_posts_navigation,et jeme réfère authème de vingt-quinze.thank you for your reply. I know paginate_links is work for me. but I want to use new function the_posts_navigation. I carefully watched get_the_posts_navigation source code, and refer to the theme of twentyfifteen.
- 0
- 2015-01-15
- Vincent Wong
-
jen'aipaspufairefonctionner `get_the_posts_pagination ()` dansma requête ajax,mais `paginate_links ()`fonctionnebien.Mercipour ça.i couldn't get `get_the_posts_pagination()` to work in my ajax query, but `paginate_links()` works fine. thanks for that.
- 0
- 2020-01-14
- honk31
WordPress a lafonction
the_posts_navigation
depuis la version 4.1.0. Maisje ne saispas comment utiliser avecwp_query
ouget_posts
. le code suivant setrouve dans unfichiermodèle depage.Méthode wp_query:
Méthodeget_posts:
Ilsne fonctionnentpaset affichent lapagination,mais saisissent http://127.0. 0.1/gdboer/? Page_id=74 & amp;page=2 manuellement dans labarre d'adresse,celafonctionne. Quipeutm'aider,mercibeaucoup!