Ottieni il numero di pagina corrente
4 risposta
- voti
-
- 2011-03-18
Quando WordPress utilizza l'impaginazionein questomodo,c'è una variabile di query
$paged
su cui sibasa. Quindi lapagina 1 è$paged=1
e lapagina 15 è$paged=15
.Èpossibile ottenereil valore di questa variabile conil codice seguente:
$paged=(get_query_var ('paged'))?get_query_var ('paged'): 1;
Ottenereilnumerototale dipagine è unpo 'più complicato. Perprima cosa devi contaretuttii postnel database. Quindifiltrain base a qualipost vengonopubblicati (rispetto a quali sonobozze,pianificati,cestino,ecc.). Quindi devi dividere questo conteggioperilnumero dipost cheprevedi vengano visualizzati su ciascunapagina:
$total_post_count=wp_count_posts (); $published_post_count=$total_post_count- >publish; $total_pages=ceil ($published_post_count/$posts_per_page);
Non l'ho ancoratestato,mapotresti dover recuperare
$posts_per_page
nello stessomodoin cui hai recuperato$paged
(usandoget_query_var () ).
When WordPress is using pagination like this, there's a query variable
$paged
that it keys on. So page 1 is$paged=1
and page 15 is$paged=15
.You can get the value of this variable with the following code:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
Getting the total number of pages is a bit trickier. First you have to count all the posts in the database. Then filter by which posts are published (versus which are drafts, scheduled, trash, etc.). Then you have to divide this count by the number of posts you expect to appear on each page:
$total_post_count = wp_count_posts(); $published_post_count = $total_post_count->publish; $total_pages = ceil( $published_post_count / $posts_per_page );
I haven't tested this yet, but you might need to fetch
$posts_per_page
the same way you fetched$paged
(usingget_query_var()
).-
Perchénon usare `$ wp_query->max_num_pages`?Why not use `$wp_query->max_num_pages`?
- 8
- 2011-03-18
- t31os
-
Questa è una valida alternativa.Parte di `$ wp_query`mi ero completamente dimenticato che c'era: -/That's a sound alternative. Part of `$wp_query` I completely forgot was there :-/
- 1
- 2011-03-18
- EAMann
-
@EAMann BTW: c'è anche laproprietà `found_posts`" Ilnumerototale diposttrovati corrispondenti aiparametri di query correnti ",wp_count_posts () è una specie di overhead.@EAMann BTW: there is also `found_posts` property "The total number of posts found matching the current query parameters", wp_count_posts() is kind of an overhead.
- 0
- 2017-03-13
- jave.web
-
- 2011-03-18
Potrestifarlo con una singola riga di codice,mapoi dinuovo,potresti voler aggiungereil codicein altriposti,quindi unafunzione di solito èpiù utile.
function current_paged( $var = '' ) { if( empty( $var ) ) { global $wp_query; if( !isset( $wp_query->max_num_pages ) ) return; $pages = $wp_query->max_num_pages; } else { global $$var; if( !is_a( $$var, 'WP_Query' ) ) return; if( !isset( $$var->max_num_pages ) || !isset( $$var ) ) return; $pages = absint( $$var->max_num_pages ); } if( $pages < 1 ) return; $page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; echo 'Page ' . $page . ' of ' . $pages; }
NOTA: il codicepuòessereinseritonelfile dellefunzioni.
Chiama semplicemente lafunzionein cui vuoimostrareilmessaggio "Pagina x di y",ades.
<?php current_paged(); ?>
Se ènecessario cheil codicefunzioni con una querypersonalizzata,ades. uno che hai creato utilizzando
WP_Query
,quindipassa semplicementeilnome della variabile che contiene la query allafunzione.Esempio di queryinesistente:
$fred = new WP_Query; $fred->query(); if( $fred->have_posts() ) ... etc..
Recupero dellapagina correnteper la querypersonalizzata utilizzando lafunzionepubblicatain precedenza ..
<?php current_paged( 'fred' ); ?>
Se vuoi dimenticare completamenteil supportoper le querypersonalizzatee stai cercando un one-liner,allora dovrebbefarlo ..
<?php echo 'Page '. ( get_query_var('paged') ? get_query_var('paged') : 1 ) . ' of ' . $wp_query->max_num_pages; ?>
Spero che questo aiuti .. :)
You could do it with a single line of code, but then again, you might want to add the code in other places, so a function is usually more useful.
function current_paged( $var = '' ) { if( empty( $var ) ) { global $wp_query; if( !isset( $wp_query->max_num_pages ) ) return; $pages = $wp_query->max_num_pages; } else { global $$var; if( !is_a( $$var, 'WP_Query' ) ) return; if( !isset( $$var->max_num_pages ) || !isset( $$var ) ) return; $pages = absint( $$var->max_num_pages ); } if( $pages < 1 ) return; $page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; echo 'Page ' . $page . ' of ' . $pages; }
NOTE: Code can go into your functions file.
Simply call the function where you want to show the "Page x of y" message, eg.
<?php current_paged(); ?>
If you need the code to work with a custom query, ie. one you've created using
WP_Query
, then simply pass along the name of the variable that holds the query to the function.Example non-existant query:
$fred = new WP_Query; $fred->query(); if( $fred->have_posts() ) ... etc..
Getting the current page for the custom query using the function posted earlier..
<?php current_paged( 'fred' ); ?>
If you want to just totally forget the custom query support and you're looking for a one-liner, then this should do it..
<?php echo 'Page '. ( get_query_var('paged') ? get_query_var('paged') : 1 ) . ' of ' . $wp_query->max_num_pages; ?>
Hope that helps.. :)
-
Sefossiin te,controllerei se `$ var` è una query:`if (is_a ($ var,'WP_Query')) `restituiràtrue se è un oggetto query.If I were you I'd check if `$var` is a query: `if( is_a( $var, 'WP_Query' ) )` will return true if it's a query object.
- 1
- 2011-03-18
- John P Bloch
-
Sì,in realtà è un'ideamolto sensata,fatto !.Yes, that's actually a very sensible idea, done!.
- 0
- 2011-04-16
- t31os
-
- 2019-02-19
Come accennatoin precedenza,unmodopiù sempliceper ottenereilnumeromassimo dipagine è con:
global $wp_query $wp_query->max_num_pages
As mentioned above - a simpler way to get the max number of pages is with:
global $wp_query $wp_query->max_num_pages
-
- 2019-11-24
Un'opzione chefunziona sututte lemie pagine di archivio è questa:
$paged_maxnum=$ GLOBALS ['wp_query'] - >max_num_pages; if ($paged_maxnum > 1) { $paged_current_page=get_query_var ('paged')?intval (get_query_var ('paged')): 1; $title_page_nrs='pagenr'. $paged_current_page. 'di'. $paged_maxnum; echo "& lt;title >" .get_the_archive_title ('',false). '-'. $title_page_nrs. '| Ilnome deltuo sito web & lt;/title > "; } altro { echo "& lt;title >" .get_the_archive_title ('',false). '| Ilnome deltuo sito web & lt;/title > "; }
Prima query se c'èpiù di unapaginain questo
wp_query
,quindi concatenailtitolo con lapagina correntetramite$paged_current_page
e lepaginetotali con$title_page_nrs . Infine
echo
. Seprima sitrova sopra 1pagina,quindielse
senon èimpaginato. Questo vanelmio archivio.php onei modelliper questotipo. Produce:& lt;title > Tassonomiatitolo -paginan. 1 di 4| Ilnome deltuo sito web & lt;/title >
o
& lt;title > Tassonomiatitolo| Ilnome deltuo sito web & lt;/title >
One option that works on all my archive pages is this:
$paged_maxnum = $GLOBALS['wp_query']->max_num_pages; if ( $paged_maxnum > 1 ) { $paged_current_page = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1; $title_page_nrs = 'page nr ' . $paged_current_page . ' of ' . $paged_maxnum; echo '<title>' . get_the_archive_title( '', false ) . ' - ' . $title_page_nrs . ' | Your Website Name</title>'; } else { echo '<title>' . get_the_archive_title( '', false ) . ' | Your Website Name</title>'; }
First query if there is more than one page in this
wp_query
, then concatenate title with current page via$paged_current_page
and total pages with$title_page_nrs
. Lastlyecho
it. If above 1 page first, thenelse
if is not paged. This goes into my archive.php or templates for this type. It produces:<title>Taxonomy title - page nr 1 of 4 | Your Website Name</title>
or
<title>Taxonomy title | Your Website Name</title>
In una situazionein cui si hanno 20postperpagina.Vorrei ottenereilnumero dipagina correnteper creare deibei collegamenti allapaginain fondo.Come si ottiene lapagina corrente.Hoprovato questo
e dice solo lapagina 1 di 1 su ognipagina.
Qualsiasiidea,
Meraviglioso