web-dev-qa-db-fra.com

wordpress Static Page pagination

En utilisant une requête post dans wordpress MAIS la pagination ne fonctionne pas, je ne sais pas quel est le problème MAIS voici mon code et je suppose que c'est correct et qu'il n'y a pas de problème

cela montre qu'il y a des pages MAIS lorsque je clique sur la page suivante, il rafraîchit la page et ne montre aucun nouveau résultat, ni la même page.

Je l’utilise sur une page statique pour devenir la page d’accueil de mon thème

<?php

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

$post_query = query_posts(array(
    'post_type'      => 'cover', // You can add a custom post type if you like
    'paged'          => $paged,
    'posts_per_page' => 1
));

?>

<?php if ( have_posts() ) : ?>

<?php
while ( have_posts() ) : the_post(); 
?>

<?php endwhile; ?>

///Pagination Function in Functions.php
<?php my_pagination(); ?>

<?php else: ?>

    No Results

<?php endif; ?>

Fonction de pagination

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

        $big = 999999999; // need an unlikely integer

        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
        ) );
    }
endif;
2
Youssef Subehi

Cette solution doit être révisée pour que les fonctions de pagination soient dans functions.php.

J'utilise le thème principal de Reverie (qui utilise le cadre de base), ce thème utilise la fonction de pagination qui se trouve dans functions.php

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

    $big = 999999999; // This needs to be an unlikely integer

    // For more options and info view the docs for paginate_links()
    // http://codex.wordpress.org/Function_Reference/paginate_links
    $paginate_links = paginate_links( array(
        'base' => str_replace( $big, '%#%', get_pagenum_link($big) ),
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages,
        'mid_size' => 5,
        'prev_next' => True,
        'prev_text' => __('&laquo;'),
        'next_text' => __('&raquo;'),
        'type' => 'list'
    ) );

    // Display the pagination if more than one page is found
    if ( $paginate_links ) {
        echo '<div class="pagination-centered">';
        echo $paginate_links;
        echo '</div><!--// end .pagination -->';
        }
    }
}

J'ai révisé cette fonction en tant que

    if( ! function_exists( 'reverie_pagination' ) ) {
    function reverie_pagination() {
        global $wp_query, $another_query;

        $big = 999999999; // This needs to be an unlikely integer
        if ( is_front_page()) {
            $myqueryis = $another_query;
            $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
            } else {
            $myqueryis = $wp_query;
            $paged = get_query_var('paged');
            }
        // For more options and info view the docs for paginate_links()
        // http://codex.wordpress.org/Function_Reference/paginate_links
        $paginate_links = paginate_links( array(
            'base' => str_replace( $big, '%#%', get_pagenum_link($big) ),
            'current' => max( 1, $paged ),
            'total' => $myqueryis->max_num_pages,
            'mid_size' => 5,
            'prev_next' => True,
            'prev_text' => __('&laquo;'),
            'next_text' => __('&raquo;'),
            'type' => 'list'
        ) );

        // Display the pagination if more than one page is found
        if ( $paginate_links ) {
            echo '<div class="pagination-centered">';
            echo $paginate_links;
            echo '</div><!--// end .pagination -->';
        }
    }
}

la variable $ another_query est mon WP_Query personnalisé. L'auteur de cette question a utilisé * query_posts * pour obtenir des résultats, mais j'ai utilisé * new WP_Query *.

Et la requête que j’ai utilisée en première page est;

 $args =  array(
            'post_type' => 'post',  
            'post__not_in'   => $do_not_duplicate,
        'paged' => $paged,
            );
 $another_query = new WP_Query( $args );
1
ewroman

Si vous avez plusieurs boucles dans la même page (par exemple, dans front_page), la pagination est parfois interrompue.

J'ai résolu le problème en utilisant le même nom de requête; Dans l'exemple ci-dessus, nous avons utilisé $myqueryis = $another_query; et dans la fonction, nous avons utilisé 'total' => $myqueryis->max_num_pages, et j'ai également transmis les arguments de requête et requête comme ceci;

$args =  array(
    'post_type' => 'post',  
    'post__not_in'   => $do_not_duplicate,
    'paged' => $paged,
        );
 $another_query = new WP_Query( $args );

avec le second exemple de boucle

$secondargs =  array(
        'post_type' => 'post',  
        'post__not_in'   => $do_not_duplicate,
        'paged' => $paged,
        'posts_per_page' => 2,  // for testing purpose to see if pagination works properly
            );
 $another_query = new WP_Query( $secondargs );

et peut-être avec la troisième exemple de boucle

$thirdargs =  array(
        'post_type' => 'post',  
        'post__not_in'   => $do_not_duplicate,
        'paged' => $paged,
        'posts_per_page' => 5,  // for testing purpose to see if pagination works properly
            );
 $another_query = new WP_Query( $thirdargs );

cela fonctionne correctement pour page/1 page/2 page/...

0
ewroman