Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

pagination with wp-pagenavi for a grid of posts in a page WordPress

  • SOLVED

Hi,

I need to show all childrens of a page in this page (not a page but a cpt "edito" in fact)

My page parent contains this code to displays the childrens :


$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$default_posts_per_page = get_option( 'posts_per_page' );

$args = array(
'post_type' => 'edito',
'post_status' => 'publish',
'posts_per_page' => $default_posts_per_page,
'order' => 'DESC',
'paged' => $paged,
'post_parent' => $post->ID,
);

$myloop = New WP_Query($args);

if( $myloop->have_posts() ) {
while ($myloop->have_posts()) : $myloop->the_post(); ?>


and at the end :
<?php endwhile;
} ?>

<section id="pagination" class="container">
<div class="row">
<?php wp_pagenavi(array('query' => $myloop)); ?>
</div>
</section>


The pagination is displayed. The links seems ok, in the HTML I can see <a href="http://mysite.com/edito/page-parent/page/2">2</a> but when I click the url of my page doesn't change.

Do you know where is my mistake ?

Thank you :)

Answers (3)

2020-05-26

Andrea P answers:

I am not sure but could you quickly try if it works by pulling the query var "page" instead of "paged":

$paged = get_query_var('page') ? get_query_var('page') : 1;

also remember to reset the query after the pagination:

wp_reset_query();

or you could try to build the pagination manually using paginate_links().

put this in your functions.php


function bwd_custom_pagination($query) {

$big = 999999999; // need an unlikely integer
$current_page = max( 1, get_query_var('paged') );

$pages_args = array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $query->max_num_pages,
);

echo paginate_links( $pages_args );
}


then in the template where you want to print out the pagination:

bwd_custom_pagination($myloop);

p.s. if the html output of the paginate_link is not as you need, there are several parameters which you can use to modify the output (maybe first look if it works): https://developer.wordpress.org/reference/functions/paginate_links/


Andrea P comments:

I have also noticed that you have a blank space between href and = in the example link you've pasted. I assume that's just a typo while writing this question, or is it like that in the actual page?


Sébastien | French WordpressDesigner comments:

Hi Andrea,

thank you for your reply. I used your function. But it still doesn't work.

The pagination works if I use the permalink "plain" format.


Andrea P comments:

which one works? the wp_pagenavi() or the custom_pagination()?

what happens if you simply add
?paged=2
to the url of the page in the url bar and press enter? is the url rewritten like /page/2 automatically?

could you try to output the value of $paged so that you can see what value is picked up and do some tests?

add the var_dump just below where you set the variable:


$paged = get_query_var('paged') ? get_query_var('paged') : 1;
var_dump($paged);


Sébastien | French WordpressDesigner comments:

with the plain format, both navigation functions work.

if I use a rewrite format for permalinks and add ?paged=2 after the url of my single, and click, ?paged=2 disappears.

va_dump return :
int(1)


Andrea P comments:

It seems that there is a redirect happening and during the redirect the query string is lost...

What happens in the url bar when you add /page/2

And could you try creating a new parent page and add some subpages? Maybe it tot bugged...

Do you have other special rewrite rules for the cpt?


Sébastien | French WordpressDesigner comments:

function cptui_register_my_cpts() {

/**
* Post Type: editos.
*/

$labels = [
"name" => __( "editos", "mysite-theme" ),
"singular_name" => __( "edito", "mysite-theme" ),
"menu_name" => __( "Edito", "mysite-theme" ),
"all_items" => __( "Toutes les pages", "mysite-theme" ),
"add_new" => __( "Ajouter une page", "mysite-theme" ),
"add_new_item" => __( "Ajouter une nouvelle page", "mysite-theme" ),
"edit_item" => __( "Editer la page", "mysite-theme" ),
"new_item" => __( "Nouvelle page", "mysite-theme" ),
"view_item" => __( "Voir la page", "mysite-theme" ),
"view_items" => __( "Voir les pages", "mysite-theme" ),
"search_items" => __( "Rechercher une page", "mysite-theme" ),
"not_found" => __( "Pas de page trouvée", "mysite-theme" ),
"not_found_in_trash" => __( "Pas de pages dans la corbeille", "mysite-theme" ),
"item_published" => __( "Page pubbliée", "mysite-theme" ),
"item_updated" => __( "Page mise à jour", "mysite-theme" ),
];

$args = [
"label" => __( "editos", "mysite-theme" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => false,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"delete_with_user" => false,
"exclude_from_search" => false,
"capability_type" => "page",
"map_meta_cap" => true,
"hierarchical" => true,
"rewrite" => [ "slug" => "edito", "with_front" => true ],
"query_var" => true,
"menu_icon" => "dashicons-welcome-widgets-menus",
"supports" => [ "title", "editor", "custom-fields", "page-attributes" ],
"taxonomies" => [ "post_tag" ],
];

register_post_type( "edito", $args );


Sébastien | French WordpressDesigner comments:

I have make a test.
My code is in single-edito.php. The pagination doesn't work.
If I paste the code of this template in another template (archive-evenements.php), the pagination works


Sébastien | French WordpressDesigner comments:

I paste the code in page.php and that works... wtf ?!


Andrea P comments:

ah!
maybe it's the parameter
"has_archive" => false,

could be that if it isn't true, then the pemalinks for pagination are skipped. I know there are some weird behaviours like that whit the has_archive and another couple of parameters.

another thing could be the
"rewrite" => [ "slug" => "edito", "with_front" => true ],

which is the front? it doesn't seem to be there in the example link you posted..


Sébastien | French WordpressDesigner comments:

I paste the code in single-actualites.php that doesn't work. In single-evenements.php, that doesn' work...

That seems linked to the template of singular...


Andrea P comments:

have you tried to change the get_query_var() to "page" rather than "paged"?


Sébastien | French WordpressDesigner comments:

finally I have find my answer myself :))

https://wordpress.stackexchange.com/questions/143812/wp-query-pagination-on-single-custom-php

add_action( 'template_redirect', function() {
if ( is_singular( 'authors' ) ) {
global $wp_query;
$page = ( int ) $wp_query->get( 'page' );
if ( $page > 1 ) {
// convert 'page' to 'paged'
$wp_query->set( 'page', 1 );
$wp_query->set( 'paged', $page );
}
// prevent redirect
remove_action( 'template_redirect', 'redirect_canonical' );
}
}, 0 ); // on priority 0 to remove 'redirect_canonical' added with priority 10

2020-05-26

Cesar Contreras answers:

I don't see any error in your code,
your site is online, can you share the link?

check the url of your page when you click, maybe if it changes but you don't have a template for it and that's why you don't notice the change, but the URL does change


Sébastien | French WordpressDesigner comments:

Hi Cesar,
my site is local.
The URL doesn't change when I click.

I use the template single-edito.php. It is a singular of the cpt "edito".
Do I need another template for the "page 2" ?


Sébastien | French WordpressDesigner comments:

The pagination works if I use the permalink "plain" format.


Cesar Contreras comments:

Do the other links work with the custom permalinks or also only with the simple configuration?

if others only works with simple setup, the problem could be your .htaccess


Cesar Contreras comments:

maybe it is obvious but I have to ask or suggest you try to activate the page navi plugin or if you have it activated, deactivate or uninstall it and come back to install it


Sébastien | French WordpressDesigner comments:

That's not a problem with the plugin. The problem exists also with the custom function of Andrea.
I have deactivate the plugin, removed the htaccess.

the pagenavi works fine elsewhere


Cesar Contreras comments:

the .htaccess should not be removed or your custom permalinks will not work


Sébastien | French WordpressDesigner comments:

yes I know. I have remove the htaccess and re-generate with backoffice.

2020-05-26

Bob answers:

which template are you using?
is your website live?

Have you tried debugging $paged variable?


Bob comments:

I think in your code keyword 'New' should be 'new'

see below
$myloop = new WP_Query($args);


Sébastien | French WordpressDesigner comments:

Hi Bob,
Thx for your response.

I use the template single-edito.php. It is a singular of the cpt "edito".
my site is local.

What do you want I do with $paged ?


I have change New into new, that doesn't change anything.


Sébastien | French WordpressDesigner comments:

The pagination works if I use the permalink "plain" format.