loop - 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 doma

I've a page named "share" with a complex loop that's working perfectly. This page is located at domain/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/share/page/2 but now it shows as domain/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 ?>

  <?
}

I've a page named "share" with a complex loop that's working perfectly. This page is located at domain/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/share/page/2 but now it shows as domain/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 edited Apr 25, 2013 at 23:04 elclanrs asked Apr 25, 2013 at 1:23 elclanrselclanrs 2333 gold badges6 silver badges16 bronze badges 8
  • 1 Perhaps I'm being stupid, but is there a particular reason that you call wp_reset_query(); before calling pagination? – netfreak Commented Apr 25, 2013 at 11:49
  • Does your theme have a front-page.php template? If so, that will override your page.php template. – mrwweb Commented Apr 25, 2013 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 Commented Apr 25, 2013 at 19:39
  • Can you enable WP_DEBUG and perhaps a dump of your $query especially when you go to the next page? – netfreak Commented Apr 25, 2013 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 Commented Apr 25, 2013 at 23:09
 |  Show 3 more comments

4 Answers 4

Reset to default 11

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

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' ),
);

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

}

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745445902a4628046.html

相关推荐

  • loop - 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 doma

    9小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信