简体   繁体   English

Wordpress / page / 2 / =找不到页面

[英]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. 我可以使用下面的代码进行分页,但是现在分页链接到/ events / page / 2 /,该链接不存在。 How do I get page 2 to work? 如何使第2页正常工作?

(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) (我有一个自定义帖子类型,称为“事件”,类别为“事件”。此类别中有8个帖子。我只能看到前5个帖子,而在第2页上找不到任何错误404)

this is from my category-event.php 这是从我的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 因为我认为URL事件/第1页无效,所以该URL应该作为参数而不是页面作为参数传递

Please installed WP-PageNavi plugin in your admin side. 请在您的管理端安装WP-PageNavi插件。

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

Now Add below code after your while loop are finish. 现在,在您的while循环完成后,添加以下代码。

<?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 分页像:上一页1 2 3下一页

<?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? 您可以尝试上面的代码吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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