admin管理员组文章数量:1122846
so been looking around for this answer, but no go -- basically i have a WP Query that is incased in a shortcode.. I have set up the paginate links function which outputs it on the page. However, the page url does not change when clicked (href does show the correct url -- /page/2) I know it is something with the main Query when executed in a shortcode. Any help would be greatly appreciated.
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'post_type' => $type,
'posts_per_page' => -1,
'orderby' => 'rand',
'paged' => $paged
);
$the_query = new WP_Query($args);
// Put default query object in a temp variable
$tmp_query = $wp_query;
// Now wipe it out completely
$wp_query = null;
// Re-populate the global with our custom query
$wp_query = $the_query;
?>
<div class="row">
<?php if ( $the_query->have_posts() ) : $i=0; ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$sphere = get_post_meta(get_the_id(),'testimonial_profession', true);
if ($i == 2) {
$i = 0;
?>
</div>
<div class="row">
<?php
}
?>
<div class="col-md-6 col-sm-12 col-xs-12">
<div class="testimonial_inner_wrapper">
<?php if(!has_post_thumbnail()): ?>
<h4 class="testimonials-inner-title"><?php the_title(); ?></h4>
<?php if(!empty($sphere)): ?>
<div class="testimonial_sphere"><?php echo($sphere); ?></div>
<?php endif; ?>
<div class="short_separator"></div>
<?php else: ?>
<div class="media">
<div class="media-left media-top">
<div class="testimonial-media-unit">
<?php the_post_thumbnail('img-69-69', array('class'=>'testimonial-media-unit-rounded')); ?>
</div>
</div>
<div class="media-body">
<h4 class="testimonials-inner-title"><?php the_title(); ?></h4>
<?php if(!empty($sphere)): ?>
<div class="testimonial_sphere"><?php echo($sphere); ?></div>
<?php endif; ?>
<div class="short_separator"></div>
</div>
</div>
<?php endif; ?>
<div class="testimonial_inner_content" style="color:#aaaaaa"><p><?php the_excerpt(); ?></p>
</div>
</div> <!-- inner wrapper -->
</div>
<?php $i++; endwhile; ?>
<!-- end of the loop -->
</div><div class="poNav">
<?php
$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' => $the_query->max_num_pages
) );
$wp_query = NULL;
$wp_query = $temp_query;
?></div>
<?php wp_reset_postdata(); ?>
<?php
$myvariable = ob_get_clean();
return $myvariable; ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;
}
so been looking around for this answer, but no go -- basically i have a WP Query that is incased in a shortcode.. I have set up the paginate links function which outputs it on the page. However, the page url does not change when clicked (href does show the correct url -- /page/2) I know it is something with the main Query when executed in a shortcode. Any help would be greatly appreciated.
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'post_type' => $type,
'posts_per_page' => -1,
'orderby' => 'rand',
'paged' => $paged
);
$the_query = new WP_Query($args);
// Put default query object in a temp variable
$tmp_query = $wp_query;
// Now wipe it out completely
$wp_query = null;
// Re-populate the global with our custom query
$wp_query = $the_query;
?>
<div class="row">
<?php if ( $the_query->have_posts() ) : $i=0; ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$sphere = get_post_meta(get_the_id(),'testimonial_profession', true);
if ($i == 2) {
$i = 0;
?>
</div>
<div class="row">
<?php
}
?>
<div class="col-md-6 col-sm-12 col-xs-12">
<div class="testimonial_inner_wrapper">
<?php if(!has_post_thumbnail()): ?>
<h4 class="testimonials-inner-title"><?php the_title(); ?></h4>
<?php if(!empty($sphere)): ?>
<div class="testimonial_sphere"><?php echo($sphere); ?></div>
<?php endif; ?>
<div class="short_separator"></div>
<?php else: ?>
<div class="media">
<div class="media-left media-top">
<div class="testimonial-media-unit">
<?php the_post_thumbnail('img-69-69', array('class'=>'testimonial-media-unit-rounded')); ?>
</div>
</div>
<div class="media-body">
<h4 class="testimonials-inner-title"><?php the_title(); ?></h4>
<?php if(!empty($sphere)): ?>
<div class="testimonial_sphere"><?php echo($sphere); ?></div>
<?php endif; ?>
<div class="short_separator"></div>
</div>
</div>
<?php endif; ?>
<div class="testimonial_inner_content" style="color:#aaaaaa"><p><?php the_excerpt(); ?></p>
</div>
</div> <!-- inner wrapper -->
</div>
<?php $i++; endwhile; ?>
<!-- end of the loop -->
</div><div class="poNav">
<?php
$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' => $the_query->max_num_pages
) );
$wp_query = NULL;
$wp_query = $temp_query;
?></div>
<?php wp_reset_postdata(); ?>
<?php
$myvariable = ob_get_clean();
return $myvariable; ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;
}
Share
Improve this question
edited May 6, 2017 at 23:48
DEM
asked May 5, 2017 at 21:53
DEMDEM
1811 silver badge9 bronze badges
1
|
1 Answer
Reset to default 0Your $args
variable would be helpful.
As I can see, you are creating custom WP_QUERY, so get_query_var('paged')
isn't related to your $the_query = new WP_QUERY
, but to global $wp_query
.
Possible solution:
declare global $paged
and add it to $args = array( /* your args here*/, 'paged' => $paged );
, also change your get_query_var('paged')
to $paged
本文标签: paginationPaginate Links in Wp Query Shortcode
版权声明:本文标题:pagination - Paginate Links in Wp Query Shortcode 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736288551a1928115.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$args
in the first line? – Milo Commented May 5, 2017 at 22:06