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.

Read More

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 ?>

  <?
}

Related posts

Leave a Reply

4 comments

  1. 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

  2. I didn’t test this, but the WP Query page says:

    Pagination Note: Use get_query_var('page'); if you want your query to work in a Page template that you’ve set as your static front page.

    So, perhaps you need this:

    $page_number = get_query_var('page') ? get_query_var('page') : 1;
    
    [ ... ]
    
    $args = array(
        'post_type' => $post_types,
        'page'      => $page_number,
        'tax_query' => array( 'relation' => 'AND' ),
    );
    
  3. I am using these code for pagination. on index.php for loop;

    <?php if ( have_posts() ) :  while ( have_posts() ) : the_post(); ?>
        // add title,excerpt or permalinks 
    <?php endwhile; ?>
        <?php pagination(); ?>
    <?php else : ?>
            <?php // no posts found message goes here ?>
    <?php endif; ?>
    

    than add following lines to functions.php

    function pagination() {
    
        if( is_singular() )
            return;
    
        global $wp_query;
    
        /** Stop execution if there's only 1 page */
        if( $wp_query->max_num_pages <= 1 )
            return;
    
        $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
        $max   = intval( $wp_query->max_num_pages );
    
        /** Add current page to the array */
        if ( $paged >= 1 )
            $links[] = $paged;
    
        /** Add the pages around the current page to the array */
        if ( $paged >= 3 ) {
            $links[] = $paged - 1;
            $links[] = $paged - 2;
        }
    
        if ( ( $paged + 2 ) <= $max ) {
            $links[] = $paged + 2;
            $links[] = $paged + 1;
        }
    
        echo '<div class="navigation"><ul>' . "n";
    
        /** Previous Post Link */
        if ( get_previous_posts_link() )
            printf( '<li>%s</li>' . "n", get_previous_posts_link() );
    
        /** Link to first page, plus ellipses if necessary */
        if ( ! in_array( 1, $links ) ) {
            $class = 1 == $paged ? ' class="active"' : '';
    
            printf( '<li%s><a href="%s">%s</a></li>' . "n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
    
            if ( ! in_array( 2, $links ) )
                echo '<li>…</li>';
        }
    
        /** Link to current page, plus 2 pages in either direction if necessary */
        sort( $links );
        foreach ( (array) $links as $link ) {
            $class = $paged == $link ? ' class="active"' : '';
            printf( '<li%s><a href="%s">%s</a></li>' . "n", $class, esc_url( get_pagenum_link( $link ) ), $link );
        }
    
        /** Link to last page, plus ellipses if necessary */
        if ( ! in_array( $max, $links ) ) {
            if ( ! in_array( $max - 1, $links ) )
                echo '<li>…</li>' . "n";
    
            $class = $paged == $max ? ' class="active"' : '';
            printf( '<li%s><a href="%s">%s</a></li>' . "n", $class, esc_url( get_pagenum_link( $max ) ), $max );
        }
    
        /** Next Post Link */
        if ( get_next_posts_link() )
            printf( '<li>%s</li>' . "n", get_next_posts_link() );
    
        echo '</ul></div>' . "n";
    
    }
    
  4. You reset the query before you generate the pagination, look these lines of your code:

    wp_reset_query();
    Theme::pagination($query);
    get_footer();
    

    I’ve not looked further more because of this. Fix this and tell if it fix the problem.