Q
Q
qwetexac2015-11-21 03:05:19
PHP
qwetexac, 2015-11-21 03:05:19

How to solve 404 pagination problem in Wordpress?

Hello, the essence of the problem is this:
There is a site on WP, there is a page where the latest news is displayed at 6 pieces per page. WP-Pagenavi plugin installed. On the first page, everything is OK, everything is displayed, but when you go to page 2+ (../news/page/2), the server throws a 404 page. The parsley itself is that I made this shaitan machine work last night, but I made a mistake somewhere and now I can’t solve the problem.
Below is the loop code on the page

<h1><?php the_title(); ?></h1>//Вывод заголовка страницы
      <?php the_content(); ?>
      <?php
      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $id = get_cat_ID( get_the_title() );//Выборка категории идет по заголовку (есть 2 страницы и 2 категории с одинаковым именем и ссылкой)
      $args=array(
        // 'post_type' => $type,
        'post_status' => 'publish',
        'paged' => $paged,
        'posts_per_page' => 6,
        'caller_get_posts'=> 1,
        'cat' => $id
        );
        $temp = $wp_query;
        $wp_query = null;
        $wp_query = new WP_Query();
        $wp_query->query($args);
        ?>
        <?php if($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post(); ?>

//Тело цикла

?php endwhile; ?>
<?php wp_pagenavi(); ?>//Вывод нумерации страниц
<?php endif; ?>

Foreshadowing incoming questions, I will give answers right away:
  1. The WP settings have a limit of 5 posts per page (less than in the loop, everything is canon)
  2. Plugin "Category pagination fix" is installed - it solved the problem last night, but now it doesn't work anymore
  3. Added code to remove "category" from address bar
add_filter('user_trailingslashit', 'remcat_function');
function remcat_function($link) {
    return str_replace("/category/", "/", $link);
}
  
add_action('init', 'remcat_flush_rules');
function remcat_flush_rules() {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}
  
add_filter('generate_rewrite_rules', 'remcat_rewrite');
function remcat_rewrite($wp_rewrite) {
    $new_rules = array('(.+)/page/(.+)/?' => 'index.php?category_name='.$wp_rewrite->preg_index(1).'&paged='.$wp_rewrite->preg_index(2));
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

The address of permalinks is as follows: %category%/%postname%
The third day I'm sitting on this garbage. Help, please, so no nerves are enough for all this!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
PorkiBi, 2020-04-26
@PorkiBi

Solved the problem like this.
$args = array(
....
'posts_per_page' => 6,
....
query_posts($args);
Settings -> reading -> Display no more than << 6 on blog pages >>
If the numbers match, then everything starts works. I don't know what kind of magic. Solved by poking...

V
Valeriy Donika, 2015-11-21
@Valonix

Try passing your query to wp_pagenavi()
wp_pagenavi( array( 'query' => $my_query ) );

A
alex1nd, 2019-03-03
@alex1nd


https://wordpress.stackexchange.com/questions/2096... helped me
Replace blog with your category name in the code above.
After adding this code, make sure you go to Settings > Permalinks and click Save to flush the rules cache, or else the rule will not be applied.

/**
 * Fix pagination on archive pages
 * After adding a rewrite rule, go to Settings > Permalinks and click Save to flush the rules cache
 */
function my_pagination_rewrite() {
    add_rewrite_rule('blog/page/?([0-9]{1,})/?$', 'index.php?category_name=blog&paged=$matches[1]', 'top');
}
add_action('init', 'my_pagination_rewrite');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question