admin管理员组文章数量:1426051
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
|
Show 3 more comments
4 Answers
Reset to default 11I 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.
本文标签: loopPagination not working on home page
版权声明:本文标题:loop - Pagination not working on home page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745445902a2658662.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
front-page.php
template? If so, that will override yourpage.php
template. – mrwweb Commented Apr 25, 2013 at 15:08front-page.php
. The content shows up fine, just like inshare
. 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:39page/2
it sayspaged=>1
?! But onshare/page/2
it sayspaged=>2
... I don't get it. – elclanrs Commented Apr 25, 2013 at 23:09