0

i have a problem with my pagination but only at category page. I'am not developer, and i try fix it from few days.

My pagination working if i make different name of pages for main BLOG, and category of BLOG, but if is the same, doesn't work... I would like set everywhere "blog/category/?strona=4" and "category/?strona=4".

For example now if i have 3 pages at main page of blog, on category doesn't work page 1 2 and 3, page 4 and more working.

Pagination MAIN BLOG:

function.php

function pagination($paged = '', $max_page = '') {
  if (!$paged) {
    $paged = get_query_var('paged');
  }
  if (!$max_page) {
    $max_page = $wp_query->max_num_pages;
  }
  
  global $wp;
  $base = home_url($wp->request);
  
  return paginate_links([
    'base' => $base . '%_%',
    'format' => '?strona=%#%',
    'current' => max(1, $paged),
    'total' => $max_page,
    'mid_size' => 2,
    'prev_text' => '<span class="pagination-prev"><img src="/wp-content/themes/naturahome/assets/images/arrow-left.png" alt=""></span>',
    'next_text' => '<span class="pagination-next"><img src="/wp-content/themes/naturahome/assets/images/arrow-right.png" alt=""></span>',
    'type' => 'list',
  ]);
} 

blog-page.php

$posts = get_posts([
  'numberposts' => -1,
  'post_type' => 'post'
]);
$paged = !empty($_GET['strona']) ? absint($_GET['strona']) : 1;
$args = [
  'numberposts' => -1,
  'paged' => $paged
];
$query = new WP_Query($args);
$mp = $query->max_num_pages;

 <section>
  <div>
    <div>
      <div class="row">
        <?php while ($query->have_posts()) : $query->the_post(); ?>
          <div class="col-sm-12">
              <?= get_template_part('partials/blog-item', null, ['one' => $post]) ?>
          </div>
        <?php endwhile; ?>
      </div>
      <?php if ($query->have_posts()) : ?>
        <div>
           <?= pagination($paged, $mp); ?>
        </div>
      <?php endif; ?>
    </div>
  </div>
</section> 

And here i have a code of category.php which working if name of pages is different than this which is in the main blog.

global $wp;
    $current_url = home_url(add_query_arg(array(), $wp->request));

    $category = get_queried_object();
    $id = $category->term_id;
    $allPosts = get_posts([
        'numberposts' => -1,
        'category' => $id,
        'post_type' => 'post'
    ]);


    $itemsCount = count($allPosts);

    $perPage = 1;


    $postsGroupsIds = [];
    $key = 1;
    if (!empty($allPosts)) {
        $i = 0;
        foreach ($allPosts as $p) {
            if (!isset($postsGroupsIds[$key])) {
                $postsGroupsIds[$key] = [];
            }

            $postsGroupsIds[$key][] = $p->ID;

            $i++;
            if ($i == $perPage) {
                $key++;
                $i = 0;
            }
        }
    }


    $pagesCount = count($postsGroupsIds);

    $pagenumber = 1;
    if (!empty($_GET['strona'])) {
        $pagenumber = $_GET['strona'];
    }

    $posts = get_posts([
        'numberposts' => $perPage,
        'post_type' => 'post',
        'include' => $postsGroupsIds[$pagenumber]
    ]);




<section class="blog-list">
    <div class="container">
        <?php foreach ($posts as $post) : ?>
        <div class="row">
            <div class="col-lg-4">
                <?php 
                $img = get_field('grafika');
                ?>
                <a href="<?= get_permalink() ?>" class="wow fadeInUp">
                    <div class="blog-list-img">
                        <?php
                    if($img) { ?>
                        <img src="<?= $img['sizes']['large'] ?>" alt="<?= $img['alt'] ?>" title="<?= $img['title'] ?>">
                        <?php
                    } else { ?>
                        <img src="<?= get_stylesheet_directory_uri(); ?>/img/default_listing.png" alt="Zaślepka">
                        <?php } ?>
                    </div>
                </a>
            </div>
            <div class="col-lg-8">
                <a href="<?= get_permalink() ?>">
                    <h4 class="blog-list-title"><?= get_the_title() ?></h4>
                </a>
                <p class="blog-list-description"><?= get_field('zajawka') ?></p>
                  <div class="date d-flex">
                    <img src="<?= get_stylesheet_directory_uri(); ?>/img/calendar.svg" alt="Data wydarzenia w House Natura">
                    <span><?= get_the_date('j F Y') ?></span>
                  </div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
</section>
                
                
<section class="section-full suggestions">
    <div class="content-wrapper container">
        <div class="row">
            <div class="col-md-12">


                <?php if (!empty($posts)) : ?>
                    <div class="pagination_outer d-new-a">
                        <?php if ($pagesCount > 1) : ?>
                            <div class="pagination_list d-new-a w100p">
                <div class="pagina_left wow fadeInUp">
                    <ul class="page-numbers">
                    <?php if ($pagenumber >= 2) : ?>
                        <li class="pagina_left wow fadeInUp">
                        <a href="<?= $current_url ?>?strona=<?= $pagenumber - 1 ?>" class="d-flex popular-category-btn custom-btn">
                            <span class="pagination-prev">
                            <img src="/wp-content/themes/naturahome/assets/images/arrow-left.png" alt="Poprzednia strona">
                            </span>
                        </a>
                        </li>
                    <?php endif; ?>

                                        <?php if ($pagesCount <= 8) : ?>
                                            <?php for ($i = 1; $i <= $pagesCount; $i++) : ?>
                                                <?php if ($pagenumber == $i) : ?>
                                                    <li><span aria-current="page" class="page-numbers current"><?= $i ?></span></li>
                                                <?php else : ?>
                                                    <li class=""><a href='<?= $current_url ?>?strona=<?= $i ?>' class="page popular-category-btn custom-btn <?= ($pagenumber == $i) ? 'current' : '' ?>" data-page="<?= $i ?>"><?= $i ?></a></li>
                                                <?php endif; ?>
                                            <?php endfor; ?>
                                        <?php else : ?>
                                            <?php if ($pagenumber <= 1) : ?>
                                                <li><span aria-current="page" class="page-numbers current"><?= $pagenumber ?></span></li>
                                                <?php for ($i = 2; $i <= 3; $i++) : ?>
                                                <li><a href='<?= $current_url ?>?strona=<?= $i ?>' class="page popular-category-btn custom-btn" data-page="<?= $i ?>"><?= $i ?></a></li>
                                                <?php endfor; ?>
                                                <li><span class="page-numbers dots">…</span></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagesCount ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagesCount ?>"><?= $pagesCount ?></a></li>

                                            <?php elseif ($pagenumber > 1 && $pagenumber <= 2) : ?>
                                                <li><a href='<?= $current_url ?>?strona=1' class="page popular-category-btn custom-btn" data-page="1">1</a></li>
                                                <li><span aria-current="page" class="page-numbers current"><?= $pagenumber ?></span></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber + 1 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber + 1 ?>"><?= $pagenumber + 1 ?></a></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber + 2 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber + 2 ?>"><?= $pagenumber + 2 ?></a></li>
                                                <li><span class="page-numbers dots">…</span></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagesCount ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagesCount ?>"><?= $pagesCount ?></a></li>

                                            <?php elseif ($pagenumber > 2 && $pagenumber <= 3) : ?>
                                                <li><a href='<?= $current_url ?>?strona=1' class="page popular-category-btn custom-btn" data-page="1">1</a></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber - 1 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber - 1 ?>"><?= $pagenumber - 1 ?></a></li>
                                                <li><span aria-current="page" class="page-numbers current"><?= $pagenumber ?></span></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber + 1 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber + 1 ?>"><?= $pagenumber + 1 ?></a></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber + 2 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber + 2 ?>"><?= $pagenumber + 2 ?></a></li>
                                                <li><span class="page-numbers dots">…</span></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagesCount ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagesCount ?>"><?= $pagesCount ?></a></li>

                                            <?php elseif ($pagenumber > 3 && $pagenumber <= 4) : ?>
                                                <li><a href='<?= $current_url ?>?strona=1' class="page popular-category-btn custom-btn" data-page="1">1</a></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber - 2 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber - 2 ?>"><?= $pagenumber - 2 ?></a></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber - 1 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber - 1 ?>"><?= $pagenumber - 1 ?></a></li>
                                                <li><span aria-current="page" class="page-numbers current"><?= $pagenumber ?></span></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber + 1 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber + 1 ?>"><?= $pagenumber + 1 ?></a></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber + 2 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber + 2 ?>"><?= $pagenumber + 2 ?></a></li>
                                                <li><span class="page-numbers dots">…</span></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagesCount ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagesCount ?>"><?= $pagesCount ?></a></li>

                                            <?php elseif ($pagenumber > ($pagesCount - 1)) : ?>
                                                <li><a href='<?= $current_url ?>?strona=1' class="page popular-category-btn custom-btn" data-page="1">1</a></li>
                                                <li><span class="page-numbers dots">…</span></li>
                                                <?php for ($i = ($pagesCount - 2); $i < $pagesCount; $i++) : ?>
                                                <li><a href='<?= $current_url ?>?strona=<?= $i ?>' class="page popular-category-btn custom-btn" data-page="<?= $i ?>"><?= $i ?></a></li>
                                                <?php endfor; ?>
                                                <li><span aria-current="page" class="page-numbers current"><?= $pagesCount ?></span></li>

                                            <?php elseif ($pagenumber > ($pagesCount - 2)) : ?>
                                                <li><a href='<?= $current_url ?>?strona=1' class="page popular-category-btn custom-btn" data-page="1">1</a></li>
                                                <li><span class="page-numbers dots">…</span></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber - 2 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber - 2 ?>"><?= $pagenumber - 2 ?></a></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber - 1 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber - 1 ?>"><?= $pagenumber - 1 ?></a></li>
                                                <li><span aria-current="page" class="page-numbers current"><?= $pagenumber ?></span></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagesCount ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagesCount ?>"><?= $pagesCount ?></a></li>

                                            <?php elseif ($pagenumber > ($pagesCount - 3)) : ?>
                                                <li><a href='<?= $current_url ?>?strona=1' class="page popular-category-btn custom-btn" data-page="1">1</a></li>
                                                <li><span class="page-numbers dots">…</span></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber - 2 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber - 2 ?>"><?= $pagenumber - 2 ?></a></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber - 1 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber - 1 ?>"><?= $pagenumber - 1 ?></a></li>
                                                <li><span aria-current="page" class="page-numbers current"><?= $pagenumber ?></span></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber + 1 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber + 1 ?>"><?= $pagenumber + 1 ?></a></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagesCount ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagesCount ?>"><?= $pagesCount ?></a></li>

                                            <?php elseif ($pagenumber > ($pagesCount - 4)) : ?>
                                                <li><a href='<?= $current_url ?>?strona=1' class="page popular-category-btn custom-btn" data-page="1">1</a></li>
                                                <li><span class="page-numbers dots">…</span></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber - 2 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber - 2 ?>"><?= $pagenumber - 2 ?></a></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber - 1 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber - 1 ?>"><?= $pagenumber - 1 ?></a></li>
                                                <li><span aria-current="page" class="page-numbers current"><?= $pagenumber ?></span></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber + 1 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber + 1 ?>"><?= $pagenumber + 1 ?></a></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber + 2 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber + 2 ?>"><?= $pagenumber + 2 ?></a></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagesCount ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagesCount ?>"><?= $pagesCount ?></a></li>

                                            <?php else : ?>
                                                <li><a href='<?= $current_url ?>?strona=1' class="page popular-category-btn custom-btn" data-page="1">1</a></li>
                                                <li><span class="page-numbers dots">…</span></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber - 2 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber - 2 ?>"><?= $pagenumber - 2 ?></a></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber - 1 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber - 1 ?>"><?= $pagenumber - 1 ?></a></li>
                                                <li><span aria-current="page" class="page-numbers current"><?= $pagenumber ?></span></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber + 1 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber + 1 ?>"><?= $pagenumber + 1 ?></a></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagenumber + 2 ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagenumber + 2 ?>"><?= $pagenumber + 2 ?></a></li>
                                                <li><span class="page-numbers dots">…</span></li>
                                                <li><a href='<?= $current_url ?>?strona=<?= $pagesCount ?>' class="page popular-category-btn custom-btn" data-page="<?= $pagesCount ?>"><?= $pagesCount ?></a></li>
                                            <?php endif; ?>
                                        <?php endif; ?>
                                    <?php if ($pagenumber < $pagesCount) : ?>
                                        <li class="pagina_right wow fadeInUp">
                                            <a href="<?= $current_url ?>?strona=<?= $pagenumber + 1 ?>" class="d-flex popular-category-btn custom-btn">
                                            <span class="pagination-next">
                                                <img src="/wp-content/themes/naturahome/assets/images/arrow-right.png" alt="Następna strona">
                                            </span>
                                            </a>
                                        </li>
                                    </ul>
                                </div>

                                <?php endif; ?>
                            </div>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</section>

enter image description here

One more information. My pagination looks like that: /%category%/%postname%

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.