admin管理员组文章数量:1208155
I am trying to make a custom page template that shows the most viewed posts. I can return posts but I am having trouble figuring out how to paginate it. Here is what I have:
$args = array('orderby' => 'meta_value_num', 'meta_key' => 'post_views_count', 'posts_per_page' => 36 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
I tried:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array('paged' => $paged, 'orderby' => 'meta_value_num', 'meta_key' => 'post_views_count', 'posts_per_page' => 36 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
but my pagination still doesn't show up. I am wondering if it has to do with my pagination function or the way I am setting up my query.
I just call my pagination function below the query, it all looks like this:
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array('paged' => $paged, 'orderby' => 'meta_value_num', 'meta_key' => 'post_views_count', 'posts_per_page' => 36 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php pagination(); ?>
This is my pagination function:
if ( ! function_exists( 'pagination' ) ) :
function pagination() {
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'mid_size' => 1,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'list'
) );
}
endif;
Any help would be greatly appreciated.
I am trying to make a custom page template that shows the most viewed posts. I can return posts but I am having trouble figuring out how to paginate it. Here is what I have:
$args = array('orderby' => 'meta_value_num', 'meta_key' => 'post_views_count', 'posts_per_page' => 36 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
I tried:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array('paged' => $paged, 'orderby' => 'meta_value_num', 'meta_key' => 'post_views_count', 'posts_per_page' => 36 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
but my pagination still doesn't show up. I am wondering if it has to do with my pagination function or the way I am setting up my query.
I just call my pagination function below the query, it all looks like this:
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array('paged' => $paged, 'orderby' => 'meta_value_num', 'meta_key' => 'post_views_count', 'posts_per_page' => 36 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php pagination(); ?>
This is my pagination function:
if ( ! function_exists( 'pagination' ) ) :
function pagination() {
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'mid_size' => 1,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'list'
) );
}
endif;
Any help would be greatly appreciated.
Share Improve this question asked Jul 12, 2014 at 16:37 justinwjustinw 1491 gold badge1 silver badge6 bronze badges 5 |3 Answers
Reset to default 5This is a local/global variable problem. It can be solved either by changing the pagination function to work with local variable, or by promoting the local variable into global scope. As you are using wp_reset_postdata()
, i guess you want to keep the original query.
Change the pagination function to accept arguments -
if ( ! function_exists( 'pagination' ) ) :
function pagination( $paged = '', $max_page = '' ) {
$big = 999999999; // need an unlikely integer
if( ! $paged ) {
$paged = get_query_var('paged');
}
if( ! $max_page ) {
global $wp_query;
$max_page = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1;
}
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, $paged ),
'total' => $max_page,
'mid_size' => 1,
'prev_text' => __( '«' ),
'next_text' => __( '»' ),
'type' => 'list'
) );
}
endif;
And the loop template will be like -
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'paged' => $paged,
'orderby' => 'meta_value_num',
'meta_key' => 'post_views_count',
'posts_per_page' => 36
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) :
$loop->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
pagination( $paged, $loop->max_num_pages); // Pagination Function
endif;
wp_reset_postdata();
Try this..
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'orderby' => 'meta_value_num',
'meta_key' => 'post_views_count',
'posts_per_page' => 36,
'paged' => $paged
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
pagination(); // Pagination Function
endif;
wp_reset_postdata();
The pagination should be added before the wp_reset_postdata();
.
I sill faced that issue and solved it like that
Pagination function
function post_pagination($paged = '', $max_page = '') {
if (!$paged) {
$paged = (get_query_var('paged')) ? get_query_var('paged') : ((get_query_var('page')) ? get_query_var('page') : 1);
}
if (!$max_page) {
global $wp_query;
$max_page = isset($wp_query->max_num_pages) ? $wp_query->max_num_pages : 1;
}
$big = 999999999; // need an unlikely integer
$html = paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, $paged),
'total' => $max_page,
'mid_size' => 1,
'prev_text' => __('« Prev'),
'next_text' => __('Next »'),
));
$html = "<div class='navigation pagination'>" . $html . "</div>";
echo $html;
}
Query on the custom template
$paged = (get_query_var('paged')) ? get_query_var('paged') : ((get_query_var('page')) ? get_query_var('page') : 1);
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => get_option('posts_per_page'),
'order' => 'DESC',
'orderby' => 'date',
'paged' => $paged
);
$loop = new WP_Query($args);
if ($loop->have_posts()) :
while ($loop->have_posts()) :
$loop->the_post();
get_template_part('content', get_post_format());
endwhile;
post_pagination($paged, $loop->max_num_pages);
endif;
wp_reset_postdata();
本文标签: functionsPagination custom query
版权声明:本文标题:functions - Pagination custom query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738691528a2107133.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$loop = new WP_Query( $args );
toquery_posts( $args )
. Also replace -$loop->have_posts() ) : $loop->the_post()
tohave_posts() ) : the_post()
. And you don't need to usewp_reset_postdata()
. – Shazzad Commented Jul 12, 2014 at 17:35query_posts
to output a custom loop. – justinw Commented Jul 12, 2014 at 17:39