La paginazione mostra gli stessi contenuti in tutte le pagine

7 feb 2014, 07:32:35
Visualizzazioni: 15.3K
Voti: 1

Ecco il mio loop

        $my_query = new WP_Query(array(
                'cat' => -399, // Esclude la categoria con ID 399
                'posts_per_page' => 6, // Mostra 6 post per pagina
                'offset' => 5, // Salta i primi 5 post
                'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1 // Ottiene il numero della pagina corrente
        ));
        if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post(); 
            $imgurl = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
            $finalurl = get_stylesheet_directory_uri(). "/functions/thumb.php?w=180&h=180&a=t&src=".$imgurl;
            $date = get_the_date(); 
            $id = $post->ID;
        ?>
        .... contenuti del loop ....

        <?php $finalurl = "";
        endwhile;
        else: ?>
        <p><?php _e('Spiacenti, nessun post corrisponde ai tuoi criteri.'); ?></p>
        <?php endif; wp_reset_query();
        $id = ""; wp_pagenavi();

La paginazione mostra lo stesso contenuto in tutte le pagine. Ho bisogno del tuo suggerimento.

0
Tutte le risposte alla domanda 4
1

Prova a cambiare $my_query in $wp_query per vedere se risolve il problema. Ho scoperto che quando rinomini la query questo interferisce con la paginazione.

Inoltre dovresti spostare il reset della query dopo la paginazione. Ecco un loop che ho verificato funzionare correttamente con la paginazione:

$args = array(
    'posts_per_page' => 10,
    'post_type'      => 'post',
    'paged'          => get_query_var( 'paged' ),
);
$wp_query = new WP_Query( $args );
while ( $wp_query->have_posts() ) : $wp_query->the_post();
    get_template_part( 'templates/content', 'posts' );
endwhile;

/*
  PAGINAZIONE
*/
if ( function_exists( 'page_navi' ) ) {
    ?>

    <div id="pagination">
        <?php page_navi(); ?>
    </div>
<?php }
wp_reset_query(); ?>
7 feb 2014 09:14:33
Commenti

Grazie, ho appena aggiunto 'paged' => get_query_var( 'paged' ), ai miei argomenti e ha risolto immediatamente il problema della paginazione.

Jesse Nickles Jesse Nickles
28 gen 2024 17:01:03
0

Se utilizzi wp_pagenavi nella pagina principale, no, non funzionerà. Devi modificare la pagina corrente nella funzione wp_pagenavi in questo modo:

// Ho modificato il codice qui sotto, puoi vedere cosa è diverso dal tuo.

if(!empty($paged)) {
    $paged = $paged;
}elseif(get_query_var( 'paged')) {
    $paged = get_query_var('paged');
}elseif(get_query_var( 'page')) {
    $paged = get_query_var('page');
}else {
    $paged = 1;
}
7 feb 2014 10:24:36
0

Ecco un miglioramento alla risposta di @Devin-Walker. Questo è specifico per archive.php ma può essere adattato per altri loop.

Inoltre utilizza i link di navigazione nativi di WordPress invece di page_navi()

Spero che questo possa essere d'aiuto a qualcuno.

<?php
/**
 * Pagina Archivio
 *
 * @Since   1.0
 */

// Esci se accesso diretto
if ( !defined('ABSPATH')) exit;
?>

<?php get_header(); ?>

<div class="container">
    <div class="row">
        <?php 

        // Nota - Imposta il numero di post per pagina in WP-Admin > Impostazioni > Lettura > 'Le pagine blog mostrano al massimo'
        $args = array(
            'post_type'                 => 'post',
            'paged'                     => get_query_var('paged'),
            'cat'                       => get_query_var('cat'),
        );

        $wp_query = new WP_Query( $args );

        while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>

            <div class="col-md-4 post-card archive my-4">

                <div class="text-center">
                    <a href="<?php the_permalink(); ?>"><img src="<?php echo the_post_thumbnail_url('card-size'); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>"></a>
                </div>
                <div class="post-card-details archive-post-details px-4 pb-2">
                    <h3 class="post-card-category px-4 py-2"><?php single_cat_title(); ?></h3>
                    <h3 class="post-card-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    <p class="post-card-p"><?php echo apply_filters( 'the_content', wp_trim_words( strip_tags( get_post_field('post_content') ), 15 ) ); ?></p>
                </div>

            </div>

        <?php endwhile; ?>

    </div>

    <!-- Navigazione -->
    <div class="tablet-pill tablet-pill-purple mb-5"><?php previous_posts_link( 'Pagina Precedente' ); ?> <?php if(get_previous_posts_link() && get_next_posts_link()) echo ' - '; ?> <?php next_posts_link( 'Pagina Successiva' ); ?></div>

    <?php wp_reset_query(); ?>

</div>

<?php get_footer();
7 ott 2017 21:00:50
0

So che è passato molto tempo, ma voglio condividere un'altra soluzione per chiunque stia riscontrando un errore simile. Nel mio caso stavo usando offset 0, quindi ho rimosso l'offset dagli argomenti della query, che era la causa del problema. Spero che possa aiutare qualcun altro:

Prima

$query_args_pagination = array(
          'post_type' => $post_type_pagination,
          // 'posts_per_page' => $countPPP,
          'post_status'    => 'publish',
          'order'          => 'ASC',
          'orderby'        => 'date',
          'posts_per_page' => '12',
          'offset'         => '0',
          'paged' => $paged_pagination,
          // 'page' => $paged_pagination,
          // 'orderby' => 'date',
          'tax_query' => array(
            array(
              'taxonomy' => $taxonomy_pagination,
              'field' => 'slug',
              'terms' => $categoria_pagination,
            )
          )
        );

Dopo

$query_args_pagination = array(
          'post_type' => $post_type_pagination,
          // 'posts_per_page' => $countPPP,
          'post_status'    => 'publish',
          'order'          => 'ASC',
          'orderby'        => 'date',
          'posts_per_page' => '12',
          // 'offset'         => '0',
          'paged' => $paged_pagination,
          // 'page' => $paged_pagination,
          // 'orderby' => 'date',
          'tax_query' => array(
            array(
              'taxonomy' => $taxonomy_pagination,
              'field' => 'slug',
              'terms' => $categoria_pagination,
            )
          )
        );
15 dic 2023 17:08:05