Pagination not working on home page

I’ve a page named “share” with a complex loop that’s working perfectly. This page is located at domain.com/share. Now I need to use this page as the home page, so I went to “Settings -> Reading” and chose that page as static page.

The problem now is that the pagination no longer works. On the actual page it shows like domain.com/share/page/2 but now it shows as domain.com/page/2 and it just loads the same content from the first page over and over no matter what page you’re in.

I didn’t do any other changes, just chose that page as front page basically. What are the possible solutions here?

I’m using WP Pagenavi if that makes any difference.

This is my page.php template:

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

if (is_page('join')) $post_types = array('articles');
if (is_page('learn')) $post_types = array('actions');
if (is_page('share')) $post_types = array('articles', 'actions');

$category = get_query_var('category');
$type = get_query_var('type');

$args = array(
  'post_type' => $post_types,
  'paged' => $paged,
  'tax_query' => array('relation' => 'AND')
);

$taxonomies = array();
foreach ($post_types as $post_type) {
  foreach (get_object_taxonomies($post_type) as $tax) {
    if (array_search($tax, $taxonomies) === false) $taxonomies[] = $tax;
  }
}

foreach ($taxonomies as $taxonomy) {

  $all_terms = get_terms($taxonomy, array('fields' => 'names'));
  $query_terms = array(ucfirst($category), ucwords(str_replace('-', ' ', $type)));
  $cur_terms = array_values(array_filter(array_intersect($all_terms, $query_terms)));

  if (! empty($cur_terms)) {
    $args['tax_query'][] = array(
      'taxonomy' => $taxonomy,
      'terms' => empty($cur_terms) ? $all_terms : $cur_terms,
      'field' => 'slug'
    );
  }
}

if (is_page('join')) $args['posts_per_page'] = 36;

$query = new WP_Query($args);

get_header();
Theme::toolbar();
?>

<div class="center">
  <div id="content">
    <? if ($query->have_posts()): ?>
      <? while ($query->have_posts()): $query->the_post() ?>
        <? get_template_part('content') ?>
      <? endwhile ?>
    <? else: ?>
    <? endif ?>
  </div>
</div>

<?
wp_reset_query();
Theme::pagination($query);
get_footer();
?>

And this is the pagination code in Theme:

function pagination($query = null)
{
  global $wp_query;
  if (empty($query)) $query = $wp_query;
  $is_paged = $query->max_num_pages > 1;
  ?>

  <? if ($is_paged): ?>
    <div class="center"><div id="pagination"><? wp_pagenavi(array('query' => $query)) ?></div></div>
  <? endif ?>

  <?
}

4 Answers
4

I had faced the same problem. And finally, I solved the problem.
Getting the current Pagination Number

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

For getting the current pagination number on a static front page (Page template) you have to use the ‘page’ query variable.

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

So you should use the get_query_var('page') instead of get_query_var('paged').
You can use is_front_page() function in if condition for frontpage query. Just as like-

<?php
if(is_front_page()) {
    $paged = (get_query_var('page')) ? get_query_var('page') : 1;
}else {
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
}

Credit goes here

Leave a Comment