简体   繁体   中英

Wordpress pagination on front-page not working

I have problem with pagination on a static front-page I have coded. This is not working setting to homepage:

<?php if ( get_query_var('paged') ) {
            $paged = get_query_var('paged'); 
        }elseif ( get_query_var('page') ) { 
            $paged = get_query_var('page'); 
        }else { 
            $paged = 1; 
        }

       $query = new WP_Query(array(
           'post_type'     =>  'post',
           'posts_per_page' => 36,
           'category_name' =>  $cat_slug[3],
           'paged'          => $paged
       ));

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

how to fix them?

Add this code after your while loop

            <div class="nav-links">
                <div class="nav-previous"><?php echo get_next_posts_link( __( 'Older Entries', 'yourtextdomain' ), $query->max_num_pages ); ?></div>
                <div class="nav-next"><?php echo get_previous_posts_link( __( 'Newer Entries', 'yourtextdomain' ) ); ?></div>
            </div>

You can get Older Entries and Newer Entries via this.

You can try as follows -

if ( get_query_var('paged') ) {
    $paged = get_query_var('paged'); 
}elseif ( get_query_var('page') ) { 
    $paged = get_query_var('page'); 
}else { 
    $paged = 1; 
}

$query = new WP_Query(array(
   'post_type'     =>  'post',
   'posts_per_page' => 36,
   'category_name' =>  $cat_slug[3],
   'paged'          => $paged
));

while ($query->have_posts()) : $query->the_post(); 
    // Print your post data
endwhile;

// Paginations
echo paginate_links( array(
    'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
    'total'        => $query->max_num_pages,
    'current'      => max( 1, get_query_var( 'paged' ) ),
    'format'       => '?paged=%#%',
    'show_all'     => false,
    'type'         => 'plain',
    'end_size'     => 2,
    'mid_size'     => 1,
    'prev_next'    => true,
    'prev_text'    => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
    'next_text'    => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
    'add_args'     => false,
    'add_fragment' => '',
) );

wp_reset_postdata();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM