{"id":231688,"date":"2022-12-22T18:15:00","date_gmt":"2022-12-22T15:15:00","guid":{"rendered":"https:\/\/wordpress.mediadoma.com\/?p=231688"},"modified":"2022-12-07T10:46:05","modified_gmt":"2022-12-07T07:46:05","slug":"impaginazione-del-tipo-di-post-personalizzato-di-wordpress","status":"publish","type":"post","link":"https:\/\/wordpress.mediadoma.com\/it\/impaginazione-del-tipo-di-post-personalizzato-di-wordpress\/","title":{"rendered":"Impaginazione del tipo di post personalizzato di WordPress"},"content":{"rendered":"<p>In generale, ogni volta che lavoro con l&#8217;impaginazione del tipo di post personalizzato, lo faccio scrivendo una query personalizzata utilizzando <a href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_query\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">WP_Query<\/a> e quindi chiamando <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_reset_postdata\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">wp_reset_postdata()<\/a> alla fine di <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>Penso ancora che questo abbia il suo posto, ma esiste una soluzione pi\u00f9 semplice che potresti essere in grado di implementare utilizzando un hook specifico fornito da WordPress.<\/p>\n<p>In particolare, sto parlando <a href=\"https:\/\/developer.wordpress.org\/reference\/hooks\/pre_get_posts\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">dell&#8217;hook pre_get_posts<\/a>. Nel Codice di Riferimento, puoi vedere che questo \u00e8 definito come:<\/p>\n<blockquote>\n<p>Si attiva dopo la creazione dell&#8217;oggetto variabile di query, ma prima dell&#8217;esecuzione della query effettiva.<\/p>\n<\/blockquote>\n<p>E poich\u00e9 la query contiene molte informazioni, possiamo andare avanti e usarla per determinare se vogliamo modificare gli argomenti necessari per gestire l&#8217;impaginazione.<\/p>\n<p>Con questo intendo dire che dobbiamo esaminare la propriet\u00e0 <strong>post_type<\/strong> e il valore <strong>posts_per_page<\/strong>. Per fare ci\u00f2, puoi implementarlo nel <a href=\"https:\/\/gist.github.com\/tommcfarlin\/1e69ad55ecdb13424a66253c7d7aa4df#file-00-pre-get-posts-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">modo seguente<\/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>Il codice sopra non \u00e8 orientato agli oggetti (se lo fosse, configurerei un abbonato e una classe responsabile della gestione della logica nella funzione), ma puoi vedere come configurarlo nella sua forma pi\u00f9 semplice.<\/p>\n<p>Quindi, se stai lavorando con l&#8217;impaginazione del tipo di post personalizzato e non vuoi occuparti dei vari aspetti dell&#8217;impostazione di una query personalizzata, questo potrebbe risolvere il tuo problema.<\/p>\n<p><div id=\"PostUnique_PostSource\" style=\"padding-top: 50px\">Fonte di registrazione:  <a target=\"_blank\" rel=\"noopener nofollow\" href=\"\/\/tommcfarlin.com\" class=\"external external_icon\">tommcfarlin.com<\/a><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Se stai lavorando con l&#8217;impaginazione del tipo di post personalizzato e non vuoi gestire una query personalizzata, questo potrebbe funzionare.<\/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":[896,835,720,865],"tags":[1168],"class_list":["post-231688","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-codice","category-guida-per-principianti","category-sviluppatore","category-wordpress-6","tag-affiai-it"],"_links":{"self":[{"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/posts\/231688","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/comments?post=231688"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/posts\/231688\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/media\/235807"}],"wp:attachment":[{"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/media?parent=231688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/categories?post=231688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/tags?post=231688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}