Hi Theirry,
I'd like to know why pagination is not working properly in my website.
<?php
global $wp_query;
$pagename = $wp_query->pagename;
$cat = single_cat_title('',false);
$order=isset($_GET['orderby']) ? $_GET['order'] : 'DSC' ;
$orderby=isset($_GET['order']) ? $_GET['orderby']: 'meta_value_num';
$displayPagination = ($_GET['pagination'] == '0') ? '-1' : '6';
$query = new WP_Query( array(
'category_name' => $cat,
'posts_per_page' => $displayPagination,
'order' => $order,
// 'nopaging' => $bool,
'meta_key' => 'release_date',
'orderby' => $orderby
) );
?>
As you can see, page 2 is not working. Can you please help why it behaves this way?
Hi Bobby,
beans_posts_pagination()
is using the global $wp_query
to handle the pagination so unless you set assign your query to it (replace $query =
with $wp_query =
in your code above) there is nothing to tell beans_posts_pagination()
about your query. Hope that helps!
Happy coding,
Hi Theirry,
Thank you for your response. I tried replacing it but the query did not work and left the posts empty. I removed global $wp_query; as I realized I won't need it. Hence, my code remains to be:
<?php
$cat = single_cat_title('',false);
$order=isset($_GET['orderby']) ? $_GET['order'] : 'DSC' ;
$orderby=isset($_GET['order']) ? $_GET['orderby']: 'meta_value_num';
$displayPagination = ($_GET['pagination'] == '0') ? '-1' : '6';
$query = new WP_Query( array(
'category_name' => $cat,
'posts_per_page' => $displayPagination,
'order' => $order,
'meta_key' => 'release_date',
'orderby' => $orderby
) );
?>
I still wonder why it doesn't find any posts in the next page. This code sits in my custom category,php. I tried removing category.php file to use the default category totem template, still, pagination won't work.
Hi Bobby,
What are you actually doing with that custom query further down, are you using it to write a custom loop? If not, are you intending to modify the main query?
Could you perhaps give us a little more details about what you are trying to achieve so that we can help and point you to the right direction.
Thanks π
Hi Theirry,
I have a custom loop that needs to display films and press (categories) in its respective pages. I am using category.php to make it possible.
In films category, I have 18 posts and I'd like to display 6 films per page. I also need to customize the look of the pagination layout to be like this http://prntscr.com/dftbkl.
Could there be something that's causing the pagination not to work?
Hey Bobby,
I am afraid but I don't follow you. If you have a category called "films" and you access your-domaine.com/category/films
it would show all the films posts by default whithout having to write a custom query.
If you want what to customize the main query, then I would strongly suggest to use WordPress core pre_get_posts
action (see documentation here), thus insuring that elements such as the pagination works smoothly. Note that this action can not be called in page templates such as category.php
but rather in functions.php
.
As mentioned earlier, Beans pagination works with the main query (like any other pagination).
Thanks,
Hi Theirry,
Thanks again for your response. I have removed the /category/ base and made your-domaine.com/category/films into your-domaine.com/films. What I aim is to customise the look of each category pages (I have films and press categories). I also use a sorting dropdown which I coded manually and updates the page based on the selection: http://prntscr.com/dgrhvi.
Thanks for clarifying and sharing about pre_get_posts. Will you have time to take a look at my category.php script and website? Please let me know so I could email access.
Thank you.
Hi Bobby,
You can customize your categories globally and/or target specific categories do have a different layout for each of them π I would suggest you to look at the WordPress Corec Template Hierarchy (here is the direct link to category templates section).
I would love to look into your current category.php
and help you with it but I unfortunately can't afford to do so as I would like to be fair and share my time to helping others too! You are welcome to ask more questions if need be though, I will gladly help with that.
Happy coding,
Thanks Theirry. I understand and I am thankful that you are very patient in responding into my questions.
Fortunately, I have found out that adding beans pagination into a non category page (category.php) makes beans pagination not to work. Thus, I added my wp query into category.php and pagination now works. I used a plugin to remove category base as well which resolved my paginaton displaying no posts on the next pages.
At this point, I'd like to know how it is possible to customise beans pagination with this new pagination layout:
The default beans pagination does not display previous link and no total number of pages.
Right now I have used this to modify the style of beans pagination which looks like this http://prntscr.com/diwmhv :
beans_add_attribute('beans_posts_pagination_item[_next]','class', 'beans_pagination_next');
beans_add_attribute('beans_posts_pagination','class', 'uk-container-center uk-container');
// Display films
beans_modify_action_callback( 'beans_posts_pagination', 'beans_posts_pagination_edit' );
function beans_posts_pagination_edit() {
/**
* Filter whether {@see beans_posts_pagination()} should be short-circuit or not.
*
* @since 1.0.0
*
* @param bool $pre True to short-circuit, False to let the function run.
*/
if ( apply_filters( 'beans_pre_post_pagination', is_singular() ) ) {
return;
}
global $wp_query;
$total = $wp_query->max_num_pages;
if ( $wp_query->max_num_pages <= 1 ) {
return;
}
$current = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$count = intval( $wp_query->max_num_pages );
beans_open_markup_e( 'beans_posts_pagination', 'div', array(
'class' => 'uk-pagination uk-grid-margin',
'role' => 'navigation',
) );
// Previous.
if ( get_previous_posts_link() ) {
beans_open_markup_e( 'beans_posts_pagination_item[_previous]', 'li' );
beans_open_markup_e( 'beans_previous_link[_posts_pagination]', 'a', array(
'href' => previous_posts( false ), // Automatically escaped.
), $current );
beans_open_markup_e( 'beans_previous_icon[_posts_pagination]', 'i', array(
'class' => 'uk-icon-long-arrow-left uk-margin-small-right',
) );
beans_close_markup_e( 'beans_previous_icon[_posts_pagination]', 'i' );
beans_output_e( 'beans_previous_text[_posts_pagination]', __( '<i class="uk-icon-long-arrow-left"></i>', 'tm-beans' ) );
beans_close_markup_e( 'beans_previous_link[_posts_pagination]', 'a' );
beans_close_markup_e( 'beans_posts_pagination_item[_previous]', 'li' );
}
// Links.
foreach ( range( 1, $wp_query->max_num_pages ) as $link ) {
// Skip if next is set.
if ( isset( $next ) && $link != $next ) {
continue;
} else {
$next = $link + 1;
}
$is_separator = array(
1 != $link, // Not first.
1 == $current && 3 == $link ? false : true, // Force first 3 items.
$count > 3, // More.
$count != $link, // Not last.
( $current - 1 ) != $link, // Not previous.
$current != $link, // Not current.
( $current + 1 ) != $link, // Not next.
);
// Separator.
if ( ! in_array( false, $is_separator ) ) {
beans_open_markup_e( 'beans_posts_pagination_item[_separator]', 'li' );
beans_output_e( 'beans_posts_pagination_item_separator_text', '...' );
beans_close_markup_e( 'beans_posts_pagination_item[_separator]', 'li' );
// Jump.
if ( $link < $current ) {
$next = $current - 1;
} elseif ( $link > $current ) {
$next = $count;
}
continue;
}
// Integer.
if ( $link == $current ) {
beans_open_markup_e( 'beans_posts_pagination_item[_active]', 'li', array( 'class' => 'uk-active' ) );
beans_open_markup_e( 'beans_posts_pagination_item[_active]_wrap', 'span' );
beans_output_e( 'beans_posts_pagination_item[_active]_text', $link );
beans_close_markup_e( 'beans_posts_pagination_item[_active]_wrap', 'span' );
beans_close_markup_e( 'beans_posts_pagination_item[_active]', 'li' );
} else {
beans_open_markup_e( 'beans_posts_pagination_item', 'li', array( 'class' => 'beans_posts_pagination_item' ) );
beans_open_markup_e( 'beans_posts_pagination_item_link', 'span' );
beans_output_e( 'beans_posts_pagination_item_link_text', $link );
beans_close_markup_e( 'beans_posts_pagination_item_link', 'span' );
beans_close_markup_e( 'beans_posts_pagination_item', 'li' );
}
}
// Next.
if ( get_next_posts_link() ) {
beans_open_markup_e( 'beans_posts_pagination_item[_next]', 'li' );
beans_open_markup_e( 'beans_next_link[_posts_pagination]', 'a', array(
'href' => next_posts( $count, false ), // Automatically escaped.
), $current );
beans_output_e( 'beans_next_text[_posts_pagination]', __( '<i class="uk-icon-long-arrow-right"></i>', 'tm-beans' ) );
beans_open_markup_e( 'beans_next_icon[_posts_pagination]', 'i', array(
'class' => 'uk-icon-long-arrow-right uk-margin-small-left',
) );
beans_close_markup_e( 'beans_next_icon[_posts_pagination]', 'i' );
beans_close_markup_e( 'beans_next_link[_posts_pagination]', 'a' );
beans_close_markup_e( 'beans_posts_pagination_item[_next]', 'li' );
}
beans_close_markup_e( 'beans_posts_pagination', 'div' );
}
I tried adding $query->max_num_posts and replaced beans_posts_pagination_item but what happened is like this:
1 / 2 3 > On the next page it looked like
Finally, this is the exact look that I need to have in my pagination: http://prntscr.com/diwnrg
Can you please help on how to achieve this? I looked at wp plugins and none really have helped achieve this.