{"id":231389,"date":"2022-12-22T18:17:00","date_gmt":"2022-12-22T15:17:00","guid":{"rendered":"https:\/\/wordpress.mediadoma.com\/?p=231389"},"modified":"2022-12-07T10:46:04","modified_gmt":"2022-12-07T07:46:04","slug":"pagination-de-type-de-message-personnalise-wordpress","status":"publish","type":"post","link":"https:\/\/wordpress.mediadoma.com\/fr\/pagination-de-type-de-message-personnalise-wordpress\/","title":{"rendered":"Pagination de type de message personnalis\u00e9 WordPress"},"content":{"rendered":"<p>De mani\u00e8re g\u00e9n\u00e9rale, chaque fois que je travaille avec une pagination de type de publication personnalis\u00e9e, cela se fait en \u00e9crivant une requ\u00eate personnalis\u00e9e \u00e0 l&rsquo;aide <a href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_query\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">de WP_Query<\/a>, puis en appelant <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_reset_postdata\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">wp_reset_postdata()<\/a> \u00e0 la fin de <a href=\"https:\/\/developer.wordpress.org\/themes\/basics\/the-loop\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">The Loop<\/a>.<\/p>\n<p>Je pense toujours que cela a sa place, mais il existe une solution plus simple que vous pourrez peut-\u00eatre mettre en \u0153uvre \u00e0 l&rsquo;aide d&rsquo;un crochet sp\u00e9cifique fourni par WordPress.<\/p>\n<p>Plus pr\u00e9cis\u00e9ment, je parle du crochet <a href=\"https:\/\/developer.wordpress.org\/reference\/hooks\/pre_get_posts\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">pre_get_posts<\/a>. Dans la r\u00e9f\u00e9rence de code, vous pouvez voir que ceci est d\u00e9fini comme\u00a0:<\/p>\n<blockquote>\n<p>Se d\u00e9clenche apr\u00e8s la cr\u00e9ation de l&rsquo;objet variable de requ\u00eate, mais avant l&rsquo;ex\u00e9cution de la requ\u00eate r\u00e9elle.<\/p>\n<\/blockquote>\n<p>Et comme la requ\u00eate contient beaucoup d&rsquo;informations, nous pouvons continuer et l&rsquo;utiliser pour d\u00e9terminer si nous voulons modifier les arguments n\u00e9cessaires pour g\u00e9rer la pagination.<\/p>\n<p>Par cela, je veux dire que nous devons examiner la propri\u00e9t\u00e9 <strong>post_type<\/strong> et la valeur <strong>posts_per_page<\/strong>. Pour ce faire, vous pouvez l&rsquo;impl\u00e9menter de <a href=\"https:\/\/gist.github.com\/tommcfarlin\/1e69ad55ecdb13424a66253c7d7aa4df#file-00-pre-get-posts-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">la mani\u00e8re suivante<\/a> :<\/p>\n<pre><code>&lt;?php\n\nuse WP_Query;\n\nadd_action('pre_get_posts', __NAMESPACE__. 'customPostTypePagination');\n\/**\n * If we're on the 'acme-post-type' page, then adjusts pagination so there are only two posts per page. This will \n * affect pagination so we can use built-in pagination links to easily page through the content.\n *\n * @param WP_Query the instance of the query object that contains information to populate the template.\n *\n * @return WP_Query an unmodified version of the query object or a modified version if we're on the acme post-type.\n *\/\nfunction customPostTypePagination(WP_Query $query)\n{\n  if (isset($query-&gt;query['post_type']) &amp;&amp; 'acme-post-type' == $query-&gt;query['post_type']) {\n    $query-&gt;set('posts_per_page', 2);\n  }\n}<\/code><\/pre>\n<p>Le code ci-dessus n&rsquo;est pas orient\u00e9 objet (si c&rsquo;\u00e9tait le cas, je configurerais un abonn\u00e9 et une classe responsable de la gestion de la logique dans la fonction), mais vous pouvez voir comment le configurer dans sa forme la plus basique.<\/p>\n<p>Donc, si vous travaillez avec une pagination de type de publication personnalis\u00e9e et que vous ne souhaitez pas vous occuper des diff\u00e9rents aspects de la configuration d&rsquo;une requ\u00eate personnalis\u00e9e, cela peut r\u00e9soudre votre probl\u00e8me.<\/p>\n<p><div id=\"PostUnique_PostSource\" style=\"padding-top: 50px\">Source d&rsquo;enregistrement:  <a target=\"_blank\" rel=\"noopener nofollow\" href=\"\/\/tommcfarlin.com\" class=\"external external_icon\">tommcfarlin.com<\/a><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Si vous travaillez avec une pagination de type de publication personnalis\u00e9e et que vous ne souhaitez pas traiter une requ\u00eate personnalis\u00e9e, cela peut fonctionner.<\/p>\n","protected":false},"author":1,"featured_media":235807,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_wp_rev_ctl_limit":""},"categories":[893,717,832,862],"tags":[1167],"class_list":["post-231389","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-code-2","category-developpeur","category-guide-pour-les-debutants","category-wordpress-3","tag-affiai-fr"],"_links":{"self":[{"href":"https:\/\/wordpress.mediadoma.com\/fr\/wp-json\/wp\/v2\/posts\/231389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.mediadoma.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.mediadoma.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/fr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/fr\/wp-json\/wp\/v2\/comments?post=231389"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.mediadoma.com\/fr\/wp-json\/wp\/v2\/posts\/231389\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/fr\/wp-json\/wp\/v2\/media\/235807"}],"wp:attachment":[{"href":"https:\/\/wordpress.mediadoma.com\/fr\/wp-json\/wp\/v2\/media?parent=231389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/fr\/wp-json\/wp\/v2\/categories?post=231389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/fr\/wp-json\/wp\/v2\/tags?post=231389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}