Impaginazione query personalizzata

12 lug 2014, 19:37:34
Visualizzazioni: 25.3K
Voti: 3

Sto cercando di creare un template di pagina personalizzato che mostri i post più visualizzati. Riesco a visualizzare i post ma ho difficoltà a implementare la paginazione. Ecco cosa ho:

$args = array('orderby' => 'meta_value_num', 'meta_key' => 'post_views_count', 'posts_per_page' => 36 );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();

Ho provato:

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; 
            $args = array('paged' => $paged, 'orderby' => 'meta_value_num', 'meta_key' => 'post_views_count', 'posts_per_page' => 36 );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();

ma la mia paginazione ancora non appare. Mi chiedo se il problema sia legato alla mia funzione di paginazione o al modo in cui ho impostato la query.

Richiamo semplicemente la mia funzione di paginazione sotto la query, ecco come appare tutto:

<?php
            $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; 
            $args = array('paged' => $paged, 'orderby' => 'meta_value_num', 'meta_key' => 'post_views_count', 'posts_per_page' => 36 );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();?>
                <?php get_template_part( 'content', get_post_format() ); ?>
            <?php endwhile; ?>
        <?php wp_reset_postdata(); ?>

<?php pagination(); ?>

Questa è la mia funzione di paginazione:

if ( ! function_exists( 'pagination' ) ) :
    function pagination() {
        global $wp_query;

        $big = 999999999; // serve un numero intero improbabile

        echo paginate_links( array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
            'current' => max( 1, get_query_var('paged') ),
            'total' => $wp_query->max_num_pages,
            'mid_size' => 1,
            'prev_text'    => __('«'),
            'next_text'    => __('»'),
            'type'         => 'list'
        ) );
    }
endif;

Qualsiasi aiuto sarebbe molto apprezzato.

5
Commenti

Sembra che tu stia eseguendo una query locale, mentre la paginazione sta utilizzando una variabile di query globale. Sostituisci questa riga - $loop = new WP_Query( $args ); con query_posts( $args ). Sostituisci anche - $loop->have_posts() ) : $loop->the_post() con have_posts() ) : the_post(). Inoltre, non hai bisogno di usare wp_reset_postdata().

Shazzad Shazzad
12 lug 2014 20:35:27

Ho sentito che non dovresti usare query_posts per produrre un loop personalizzato.

justinw justinw
12 lug 2014 20:39:29

Questa è la best practice. Utilizzare la paginazione globale ( get_query_var('paged') ) in una query personalizzata non è nemmeno una best practice. Comunque, sto scrivendo una risposta tra pochi secondi.

Shazzad Shazzad
12 lug 2014 20:45:14

Ho trovato un post su questo. Utilizzando quel metodo funziona abbastanza bene.

justinw justinw
12 lug 2014 20:46:35

Ho trovato la soluzione: Paginazione dei post WordPress

Fefar Ravi Fefar Ravi
5 ott 2021 19:37:36
Tutte le risposte alla domanda 3
1

Questo è un problema di variabile locale/globale. Può essere risolto modificando la funzione di paginazione per lavorare con una variabile locale, oppure promuovendo la variabile locale in ambito globale. Dato che stai usando wp_reset_postdata(), immagino tu voglia mantenere la query originale.

Modificare la funzione di paginazione per accettare argomenti -

if ( ! function_exists( 'pagination' ) ) :

    function pagination( $paged = '', $max_page = '' ) {
        $big = 999999999; // serve un numero improbabile
        if( ! $paged ) {
            $paged = get_query_var('paged');
        }

        if( ! $max_page ) {
            global $wp_query;
            $max_page = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1;
        }

        echo paginate_links( array(
            'base'       => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format'     => '?paged=%#%',
            'current'    => max( 1, $paged ),
            'total'      => $max_page,
            'mid_size'   => 1,
            'prev_text'  => __( '«' ),
            'next_text'  => __( '»' ),
            'type'       => 'list'
        ) );
    }
endif;

E il template del loop sarà così -

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; 
$args = array(
    'paged'          => $paged, 
    'orderby'        => 'meta_value_num', 
    'meta_key'       => 'post_views_count', 
    'posts_per_page' => 36 
);
$loop = new WP_Query( $args );

if ( $loop->have_posts() ) :
    while ( $loop->have_posts() ) : 
        $loop->the_post();
        get_template_part( 'content', get_post_format() );
    endwhile;

    pagination( $paged, $loop->max_num_pages); // Funzione di Paginazione
endif;

wp_reset_postdata();
12 lug 2014 20:49:22
Commenti

Mille grazie

Muhammad Mehran Khan Attari Muhammad Mehran Khan Attari
11 set 2020 14:26:41
1

Prova questo..

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
    'orderby' => 'meta_value_num',
    'meta_key' => 'post_views_count', // Ordina per conteggio visualizzazioni
    'posts_per_page' => 36, // Numero di post per pagina
    'paged' => $paged // Pagina corrente
    );

$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
    while ( $loop->have_posts() ) : $loop->the_post();

            get_template_part( 'content', get_post_format() ); // Carica il template del contenuto

    endwhile;

    pagination(); // Funzione di paginazione

endif;
wp_reset_postdata(); // Resetta i dati del post

La paginazione dovrebbe essere aggiunta prima del wp_reset_postdata();.

12 lug 2014 19:58:07
Commenti

Sfortunatamente, non ha paginato.

justinw justinw
12 lug 2014 20:21:25
0

Ho affrontato lo stesso problema e l'ho risolto in questo modo

Funzione di paginazione

function post_pagination($paged = '', $max_page = '') {
    if (!$paged) {
        $paged = (get_query_var('paged')) ? get_query_var('paged') : ((get_query_var('page')) ? get_query_var('page') : 1);
    }

    if (!$max_page) {
        global $wp_query;
        $max_page = isset($wp_query->max_num_pages) ? $wp_query->max_num_pages : 1;
    }

    $big  = 999999999; // bisogno di un intero improbabile

    $html = paginate_links(array(
        'base'       => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
        'format'     => '?paged=%#%',
        'current'    => max(1, $paged),
        'total'      => $max_page,
        'mid_size'   => 1,
        'prev_text'  => __('« Prec'),
        'next_text'  => __('Succ »'),
    ));

    $html = "<div class='navigation pagination'>" . $html . "</div>";

    echo $html;
}

Query sul template personalizzato

$paged = (get_query_var('paged')) ? get_query_var('paged') : ((get_query_var('page')) ? get_query_var('page') : 1);

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => get_option('posts_per_page'),
    'order' => 'DESC',
    'orderby' => 'date',
    'paged' => $paged
);

$loop = new WP_Query($args);
if ($loop->have_posts()) :
    while ($loop->have_posts()) :
        $loop->the_post();
        get_template_part('content', get_post_format());
    endwhile;

    post_pagination($paged, $loop->max_num_pages);
endif;

wp_reset_postdata();
3 mar 2022 22:18:27