Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

  <?
}
share|improve this question
1  
Perhaps I'm being stupid, but is there a particular reason that you call wp_reset_query(); before calling pagination? –  netfreak Apr 25 '13 at 11:49
    
Does your theme have a front-page.php template? If so, that will override your page.php template. –  mrwweb Apr 25 '13 at 15:08
    
@mrwweb: Nop, no front-page.php. The content shows up fine, just like in share. The pagination shows up but all the pages load the content from the first one. I have infinite scroll, so it gets stuck and loads the same content over and over infinitely. –  elclanrs Apr 25 '13 at 19:39
    
Can you enable WP_DEBUG and perhaps a dump of your $query especially when you go to the next page? –  netfreak Apr 25 '13 at 22:49
    
@netfreak: I have WP_DEBUG enabled. I just checked the query closely again, and in page/2 it says paged=>1?! But on share/page/2 it says paged=>2... I don't get it. –  elclanrs Apr 25 '13 at 23:09

3 Answers 3

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' ),
);
share|improve this answer

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";

}
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.