Forums › Forums › Search & Filter Pro › Custom Post Type AJAX Pagination – Very Close to working !
- This topic has 8 replies, 3 voices, and was last updated 8 years, 1 month ago by
Ross.
-
Anonymous(Private) March 20, 2017 at 2:26 pm #97969
Hello, and thank you for the great plugin. We’ve spent some time customizing things and believe that we’re very close to having things buttoned up.
The one snag that I’m not able to get around is getting our numbered pagination to work quite right, although it is very close. When the page loads, the paginated menu exists and all of the links work correctly. But upon refresh, the paginated menu is not being updated with the “current” menu item and updated links/etc. I scoured the forum and tried a number of different approaches, but am now reaching out for a bit of support to see what I have missed.
Here’s the dev page that we’d like to AJAX paginate:
[redacted]
Here’s our function that draws the pagination:
// Output AJAX Navigation With Bootstrap Markup function ajax_page_navi() { global $the_query; $big = 999999999; // need an unlikely integer $pages = paginate_links( array( 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 'format' => '?sf_paged=%#%', 'current' => max( 1, get_query_var('paged') ), 'total' => $the_query->max_num_pages, 'prev_next' => false, 'type' => 'array', 'prev_next' => TRUE, 'prev_text' => __('<i class="fa fa-lg fa-angle-left"></i> <span>Prev</span>'), 'next_text' => __('<span>Next</span> <i class="fa fa-lg fa-angle-right"></i>'), ) ); if( is_array( $pages ) ) { $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged'); echo '<ul class="pagination">'; foreach ( $pages as $page ) { echo "<li>$page</li>"; } echo '</ul>'; } } /* end page navi */
Then, we’re using a custom page template (page-recipes.php) to do our custom post type query:
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; // loop arguments $arr = array( 'post_type' => 'recipe', 'paged' => $paged, 'search_filter_id' => 1664 ); global $the_query; $the_query = new WP_Query( $arr ); ?>
Any ideas about what I might be overlooking here?
Thanks in advance,
Ross
Anonymous(Private) March 20, 2017 at 3:21 pm #97993Oh…and I updated our page navigation as follows, note the ‘current’ value:
// Output AJAX Navigation With Bootstrap Markup function ajax_page_navi() { global $the_query; $big = 999999999; // need an unlikely integer $pages = paginate_links( array( 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 'format' => '?sf_paged=%#%', 'current' => max( 1, $the_query->query_vars['paged'] ), 'total' => $the_query->max_num_pages, 'prev_next' => false, 'type' => 'array', 'prev_next' => TRUE, 'prev_text' => __('<i class="fa fa-lg fa-angle-left"></i> <span>Prev</span>'), 'next_text' => __('<span>Next</span> <i class="fa fa-lg fa-angle-right"></i>'), ) ); if( is_array( $pages ) ) { $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged'); echo '<ul class="pagination">'; foreach ( $pages as $page ) { echo "<li>$page</li>"; } echo '</ul>'; } } /* end page navi */
Anonymous(Private) March 20, 2017 at 5:46 pm #98060Hello…yes, I believe we found a solution that will work. For future reference, does my explanation for what I changed sound like a good way to go?
I’m stumped as to why this works:
'current' => max( 1, $the_query->query_vars['paged'] ),'
But this doesn’t:
'current' => max( 1, get_query_var('paged') ),
Any thoughts?
Ross Moderator(Private) March 21, 2017 at 3:39 pm #98305Yup makes sense.
Basically all the examples in the codex use:
'current' => max( 1, get_query_var('paged') ),
I think this is actually wrong for many scenarios.
get_query_var
get the global paged var, but depending on how you use S&F (and WP, ie doing a custom query, with pagination), you will need to use'current' => max( 1, $the_query->query_vars['paged'] ),'
– this get the current value from the query, not the global valueTo explain a little more.
If you are creating a custom query on and individual page, the global paged var will always be
1
. Because the global query is the request to get that page.Then you have the custom query, which of course has a different value. I would advise (and planned to submit to the codex) to always get the current page var from the query.
If its a global query, why not still use
global $wp_query
and then use the same coding style.Anyway, hope that clarifies a little bit.
Best
-
AuthorPosts