Création d'une pagination simple pour les modèles de type de publication personnalisés
1 réponses
- votes
-
- 2015-02-01
posts_nav_link()
ne fonctionnerapasiciUtilepourfournir unenavigation "paginée" despages d'index,de catégorieet d'archive.
Vous devrez utiliser le
next_posts_link()
plusgénériqueetprevious_posts_link()
. Assurez-vous simplement de définir leparamètre$max_pages
pournext_posts_links()
sinon celane fonctionnerapas,et gardez à l'esprit que si,parmodification,vous utilisez cettepage comme statiquepage d'accueil,alors vous devez utiliserpage
,paspaged
MODIFIER 1
Utilisez les liensmentionnés ci-dessus comme suit
next_posts_link( 'Older Entries', $loop->max_num_pages ); previous_posts_link( 'Newer Entries' );
MODIFIER 2
Jepense qu'il y a un légermalentendu. Essayez ce qui suit:
-
Dans vos arguments detype depublicationpersonnalisés où vousenregistrez votretype depublicationpersonnalisé,ajoutez ce qui suit,
'has_archive' => true
. Votrefonction devrait ressembler à cecifunction custom_post_news() { register_post_type( 'news', array('labels' => array( 'name' => __('News', 'post type general name'), /* This is the Title of the Group */ 'singular_name' => __('News', 'post type singular name'), /* This is the individual type */ 'add_new' => __('Add New', 'custom post type item'), /* The add new menu item */ 'add_new_item' => __('Add New'), /* Add New Display Title */ 'edit' => __( 'Edit' ), /* Edit Dialog */ 'edit_item' => __('Edit'), /* Edit Display Title */ 'new_item' => __('New '), /* New Display Title */ 'view_item' => __('View'), /* View Display Title */ 'search_items' => __('Search news'), /* Search Custom Type Title */ 'not_found' => __('Nothing found in the Database.'), /* This displays if there are no entries yet */ 'not_found_in_trash' => __('Nothing found in Trash'), /* This displays if there is nothing in the trash */ 'parent_item_colon' => '' ), /* end of arrays */ 'description' => __( 'This is the example custom post type' ), /* Custom Type Description */ 'public' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'show_ui' => true, 'query_var' => true, 'menu_position' => 2, /* this is what order you want it to appear in on the left hand side menu */ 'capability_type' => 'post', 'hierarchical' => false, 'rewrite' => array('slug' => 'news', 'with_front' => true ), 'has_archive' => true, /* the next one is important, it tells what's enabled in the post editor */ 'supports' => array( 'title', 'editor', 'thumbnail') ) ); }
-
Videz vospermaliens après cet ajout,puis visitez lapage d'accueil
-
Créez un
archive-news.php
. Cela devrait ressembler à ceci<?php while ( have_posts() ) : the_post(); ?> // Loop <?php endwhile; ?> <div id="pagination" class="clearfix"> <?php posts_nav_link(); ?> </div>
-
Ensuite,dans votrefunctions.php,utilisez
pre_get_posts
pourmodifier la requêteprincipale sinécessaire.add_action( 'pre_get_posts', function ( $q ) { if( !is_admin() && $q->is_main_query() && $q->is_post_type_archive( 'news' ) ) { $q->set( 'posts_per_page', 2 ); } });
-
Supprimez lapage que vous avez créée dans leback-end. Tout devraitfonctionnerparfaitementmaintenant
posts_nav_link()
won't work hereUseful for providing "paged" navigation of index, category and archive pages.
You will have to use the more generic
next_posts_link()
andprevious_posts_link()
. Just make sure to set the$max_pages
parameter fornext_posts_links()
otherwise it will not work, and keep in mind, if by change, you are using this page as a static front page, then you should usepage
, notpaged
EDIT 1
Use the above mentioned links as follows
next_posts_link( 'Older Entries', $loop->max_num_pages ); previous_posts_link( 'Newer Entries' );
EDIT 2
I think there is a slight misunderstanding. Try the following:
In your custom post type arguments where you register your custom post type, add the following,
'has_archive' => true
. Your function should look like thisfunction custom_post_news() { register_post_type( 'news', array('labels' => array( 'name' => __('News', 'post type general name'), /* This is the Title of the Group */ 'singular_name' => __('News', 'post type singular name'), /* This is the individual type */ 'add_new' => __('Add New', 'custom post type item'), /* The add new menu item */ 'add_new_item' => __('Add New'), /* Add New Display Title */ 'edit' => __( 'Edit' ), /* Edit Dialog */ 'edit_item' => __('Edit'), /* Edit Display Title */ 'new_item' => __('New '), /* New Display Title */ 'view_item' => __('View'), /* View Display Title */ 'search_items' => __('Search news'), /* Search Custom Type Title */ 'not_found' => __('Nothing found in the Database.'), /* This displays if there are no entries yet */ 'not_found_in_trash' => __('Nothing found in Trash'), /* This displays if there is nothing in the trash */ 'parent_item_colon' => '' ), /* end of arrays */ 'description' => __( 'This is the example custom post type' ), /* Custom Type Description */ 'public' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'show_ui' => true, 'query_var' => true, 'menu_position' => 2, /* this is what order you want it to appear in on the left hand side menu */ 'capability_type' => 'post', 'hierarchical' => false, 'rewrite' => array('slug' => 'news', 'with_front' => true ), 'has_archive' => true, /* the next one is important, it tells what's enabled in the post editor */ 'supports' => array( 'title', 'editor', 'thumbnail') ) ); }
Flush your permalinks after this addition and then visit the homepage
Create an
archive-news.php
. It should look like this<?php while ( have_posts() ) : the_post(); ?> // Loop <?php endwhile; ?> <div id="pagination" class="clearfix"> <?php posts_nav_link(); ?> </div>
Next, in your functions.php, use
pre_get_posts
to alter the main query as needed.add_action( 'pre_get_posts', function ( $q ) { if( !is_admin() && $q->is_main_query() && $q->is_post_type_archive( 'news' ) ) { $q->set( 'posts_per_page', 2 ); } });
Delete the page that you have created in the back end. Everything should work perfectly now
-
Désolé,je vois que vous venez de commenterjuste unje l'ai compris,doncje les ai supprimés.Quoi qu'ilen soit,mercipour lesmises àjour.Celafonctionneen termes d'affichage du lien «Entréesplus anciennes»,maisil renvoie à lapageindex.phpSorry, I see you have just commented just a I figured it out so I deleted them. Anyway, thanks for the updates. It works in terms of showing the 'Older entries' link, but it links back to the index.php page
- 0
- 2015-02-01
- Jack Barham
-
Cela a quelque chose à voir avec vos règles de réécriture.Avez-vous vidé vospermaliensThat has something to do with your rewrite rules. Did you flush your permalinks
- 0
- 2015-02-01
- Pieter Goosen
-
Opps,j'aipensé que celaformateraitOpps, thought that would format
- 0
- 2015-02-01
- Jack Barham
-
Ce code (deuxièmebloc de code dans la question)est déjà dansmonfunctions.php -j'aifait un autre vidageen réenregistrant lapage deparamètres despermalienset nefonctionnetoujourspas.That code (second code block in the question) is already in my functions.php - I've done another flush by re-saving the permalinks settings page and still not working.
- 0
- 2015-02-01
- Jack Barham
-
Bien sûr,j'ai supprimé ce commentairemaintenant.J'ai séparé lafin pendantet je l'aiplacée après laboucle,puisj'ai ajoutémespost_links suivantet précédent,puisje l'aifermé avec wp_reset_postdata ();vidé le cacheet toujoursfrapper l'index.phpSure, I deleted that comment now. I've separated the endwhile and placed it after the loop, then added my next and prev post_links then closed it with wp_reset_postdata(); flushed the cache and still hitting the index.php
- 0
- 2015-02-01
- Jack Barham
-
Vous légende!Ça amarché.C'est ce quej'aifait (pour aider quelqu'un d'autre).Utilisé votre code suivant/précédent.Ajout de has_archive=>true à register_post_typepuis déplacétout le code depage-news.php vers archive-news.phpet cela afonctionné.Apprécierton aide.You legend! It's worked. This is what I did (to help anyone else). Used your next/prev code. Added has_archive => true to register_post_type and then moved all the code from page-news.php to archive-news.php and it's worked. Appreciate your help.
- 0
- 2015-02-01
- Jack Barham
-
Vérifiez simplementmamise àjour,je viens defaire lamême chose dans unemodification avec labonnefaçon de lefaire.Je vaisme couchermaintenant.J'espère quemamise àjour vous aidera également.Prendreplaisir :-)Just check my update, I have just done the same in an edit with the proper way to do it. I'm off to bed now. Hope my update also helps you. Enjoy :-)
- 0
- 2015-02-01
- Pieter Goosen
J'ai unmessagepersonnalisé detype "news"et j'ai dumal à lefairepaginer correctement. Je recherche un simple lienprécédentet suivant,sansm'inquiéter desnuméros depage aumilieu.
Danspage-news.php,voicimon code:
Le code ci-dessus affiche 2 articles,mais aucun lien depagination
Voicimon code detype demessagepersonnalisé:
C'est unthèmepersonnalisé (commencé àpartir de zéro) donciln'y a rien dansmonfunctions.phpfaisant référence à unepagination. J'aiparcouru Internetpourtrouver des réponses,maisj'obtiens une solution différenteet rienne semblefonctionner.
Jen'aipasnonplus d'archive-news.php carje ne saispas si cela doit êtreimplémenté ounon?