简体   繁体   中英

how to show pagination in wordpress shortcode?

hi i am working on wp_query for wordpress its works fine for me. Pagination is working on template pages but when i create a shortcode and try to display the pagination it is not working but the same code is working on template files here is my query

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$wp_query = new WP_Query();
$wp_query->query('cat=4&showposts=5'.'&paged='.$paged);

content display code

<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php the_title(); ?>

and finaly the pagination

<?php endwhile; ?>
<div class="twf_pagination">
<?php echo paginate_links( $args ); ?>
</div>

anyone can tell me what i am doing wrong? and how can i fix it? thanx

You are completely breaking your main query here. You have the following issues here:

  • Don't use the variable $wp_query . This is the global variable used and set by the main query. Rather use a variable like $my_query . Check out the codex, WP_Query to see how to construct a proper custom query

  • Nowhere do I see that you are resetting the postdata. Use wp_reset_postdata() diretly after your pagination function

  • Remember, the output from a shortcode should be returned, not echod. Either use a buffer to save your output and return that at the end or add your output to a variable and return that variable. Check out the Shortcode API and learn how to properly construct a shortcode

  • Check out the codex, paginate_links() on how to properly use the use function with custom queries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM