mobile buddy
mobile buddy

Reputation: 11

Wordpress Pagination not working - Page url changes but content doesn't change

Homepage displays all the post and I want pagination for this. After clicking on the pagination link page number changes in url but same content is shown on all pages, also the pagination link which number shows only 1 selected. Like if i selcted pagination link 3, url shown page/3, but page number is 1 and content doesnt even change.

enter image description here

enter image description here

<?php
/**
 * Genesis Sample.
 *
 * This file adds the landing page template to the Genesis Sample Theme.
 *
 * Template Name: Home
 *
 * @package Genesis Sample
 * @author  StudioPress
 * @license GPL-2.0-or-later
 * @link    https://www.studiopress.com/
 */
get_header();

$paged = (isset($_GET['paged'])) ? intval($_GET['paged']) : 1;
$paged = (get_query_var('paged')) ? get_query_var('paged') : $paged;


$args = array(
  'post_type' => 'post',
  'post_status' => 'publish',
  'posts_per_page' => 9,
  'paged' => $paged,
);
$wp_query = new WP_Query($args);
?>

<div class="body-container">
  <div class="blogs-banner">
    <?php
    $image = get_field('image');
    if (!empty($image)) : ?>
      <img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" class="img-main" />
    <?php endif; ?>
  </div>
  <div class="about-container">
    <div class="blog-list">
      <div class="row-same-height" id="postContainer" data-type="post" data-count="<?php echo $wp_query->found_posts; ?>">
        <?php
        if ( $wp_query->have_posts() ) :
          while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
            <a href="" class="link-post">
              <div class="col-same-height">
                <div class="content">
                  <div class="TopArticleCard-Container">
                    <div class="img-container">
                      <?php
                      $thumbnail = get_post_meta(get_the_id(), 'large_img', true);
                      if ( $thumbnail ) { ?>
                        <img src="<?php echo $thumbnail; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" loading="lazy"/>
                      <?php } else if ( has_post_thumbnail() ) {
                        the_post_thumbnail('large', ['title' => get_the_title()], ['alt' => get_the_title()]); ?>
                      <?php
                      } else { ?>
                        <img src="<?php echo get_site_url(); ?>/wp-content/uploads/2020/09/default.jpg" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" loading="lazy"/>
                      <?php } ?>
                    </div>
                    <div class="article-description">
                      <div class="article-tag">
                        <?php $cat = get_the_category(); echo $cat[0]->cat_name; ?>
                      </div>
                      <div class="content">
                        <div class="article-title">
                          <?php the_title(); ?>
                        </div>
                     </div>
                </div>
              </div>
            </div>
          </div>
        </a>
        <?php endwhile;
    // post_pagination($query);
  endif;
  wp_reset_postdata();
  ?>
</div>
<nav class="pagination-nav">
  <ul class="pagination">
    <?php
    if ( $wp_query->have_posts() ) :
      $currentPage = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $pagination_args = array(
        'base' => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
        'format' => '/page/%#%',
        'current' => $currentPage,
        'total' => $wp_query->max_num_pages,
        'prev_text' => 'Prev',
        'next_text' => 'Next',
      );

      echo paginate_links(array_merge($pagination_args, array('query' => $wp_query)));
    endif;
    ?>
  </ul>
</nav>





      </div>
    </div>



  </div>
</div>

<?php get_footer();

Upvotes: 0

Views: 712

Answers (1)

Silvia Arzamendia
Silvia Arzamendia

Reputation: 1

I got mine working by adding one line in the initial query array:

'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),

it looked like this:

global $wp_query;


query_posts(array(
    'post_type'             => 'gallery',
    'post_status'           => 'publish',
    'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
    'order'                     => 'DESC',
));

I thought my problem was in the pagination, but, it was in the initial call to the query

Upvotes: 0

Related Questions