简体   繁体   中英

Wordpress /page/2/ = Page Not Found

I got pagination to work with the below code, but now the pagination links to /events/page/2/, which doesn't exist. How do I get page 2 to work?

(I have a custom post type called 'events' and the category called 'event'. There're 8 posts in this category. I can see only the first 5 ones and found nothing on page 2 with error 404)

this is from my category-event.php

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'posts_per_page' => 5,
    'cat'      => 2, // category: product
    'order'    => 'DESC',
    'paged'    => $paged,
    'meta_query' => array(                  //(array) - Custom field parameters
        array(
            'key' => 'give_away_event',     //(string) - Custom field key.
            'value' => 'Active',            //(string/array) - Custom field value (Note: Array support is limited to a compare value of 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN')
            'type' => 'CHAR',               //(string) - Custom field type. Possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. Default value is 'CHAR'.
            'compare' => '=',               //(string) - Operator to test. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. Default value is '='.
        )
    ),
);

query_posts( $args );
$myposts = get_posts( $args );

Do you use

next_posts_link( 'Older Entries', $loop->max_num_pages );
previous_posts_link( 'Newer Entries' );

because I think the URL events/page/1 is invalid the URL should pass paged as a paramter, not page

Please installed WP-PageNavi plugin in your admin side.

Plugin URL : https://wordpress.org/plugins/wp-pagenavi/

Now Add below code after your while loop are finish.

<?php wp_pagenavi( array( 'query' => $queryall ) ); ?>  /* This code will generate pagination */

Example :

<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'posts_per_page' => 10,
    'paged'          => $paged,
    'offset' => 0,
    'tax_query' => array(
        array(
            'taxonomy' => 'videos',
            'field'    => 'term_id',
            'terms'    => $catid,
        ),
    ),
    'orderby' => 'rand',
    'post_type' => 'video',
    'post_status' => 'publish'
);
$queryall = new WP_Query($args);

if ($queryall->have_posts()) :  
    while ($queryall->have_posts()) : $queryall->the_post();

 endwhile;
endif;
 ?>
<div class="cat-pagination">
    <?php wp_pagenavi( array( 'query' => $queryall ) ); ?>
</div>

Pagination Like : Prev 1 2 3 Next

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

$data= new WP_Query(array(
    'post_type'=>’YOUR_POST_TYPE’, // your post type name
    'cat'      => 2, // category: product
    'order'    => 'DESC',
    'posts_per_page' => 3, // post per page
    'paged' => $paged,
    'meta_query' => array(                  //(array) - Custom field parameters
        array(
            'key' => 'give_away_event',     //(string) - Custom field key.
            'value' => 'Active',            //(string/array) - Custom field value (Note: Array support is limited to a compare value of 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN')
            'type' => 'CHAR',               //(string) - Custom field type. Possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. Default value is 'CHAR'.
            'compare' => '=',               //(string) - Operator to test. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. Default value is '='.
        )
    ),
));

if($data->have_posts()) :
    while($data->have_posts())  : $data->the_post();
            // Your code
    endwhile;

    $total_pages = $data->max_num_pages;

    if ($total_pages > 1){

        $current_page = max(1, get_query_var('paged'));

        echo paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => '/page/%#%',
            'current' => $current_page,
            'total' => $total_pages,
            'prev_text'    => __('« prev'),
            'next_text'    => __('next »'),
        ));
    }
    ?>    
<?php else :?>
<h3><?php _e('404 Error&#58; Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>

Could you please try above code?

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