diff --git a/packages/block-library/src/query-pagination-next/index.php b/packages/block-library/src/query-pagination-next/index.php
index 05e00a108ffe8..2011e1e4730a6 100644
--- a/packages/block-library/src/query-pagination-next/index.php
+++ b/packages/block-library/src/query-pagination-next/index.php
@@ -25,42 +25,56 @@ function render_block_core_query_pagination_next( $attributes, $content, $block
$default_label = __( 'Next Page' );
$label = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? $attributes['label'] : $default_label;
$pagination_arrow = get_query_pagination_arrow( $block, true );
+ $content = '';
if ( $pagination_arrow ) {
$label .= $pagination_arrow;
}
- $content = sprintf(
- '%2$s',
- $hidden_wrapper_attributes,
- $label
- );
-
// Check if the pagination is for Query that inherits the global context.
if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) {
+ global $wp_query;
+ // Take into account if we have set a bigger `max page`
+ // than what the query has.
+ $max_page = ! $max_page || $max_page > $wp_query->max_num_pages ? $wp_query->max_num_pages : $max_page;
$filter_link_attributes = function() use ( $wrapper_attributes ) {
return $wrapper_attributes;
};
add_filter( 'next_posts_link_attributes', $filter_link_attributes );
- // Take into account if we have set a bigger `max page`
- // than what the query has.
- global $wp_query;
- if ( $max_page > $wp_query->max_num_pages ) {
- $max_page = $wp_query->max_num_pages;
- }
- if ( (int) $wp_query->max_num_pages !== $paged ) {
- $content = get_next_posts_link( $label, $max_page );
+
+ // If there are pages to paginate.
+ var_dump( $wp_query->max_num_pages );
+ if ( 1 < $max_page ) {
+ if ( (int) $max_page !== $paged ) { // If we are NOT in the last one.
+ $content = get_next_posts_link( $label, $max_page );
+ } else { // If we are in the last one.
+ $content = sprintf(
+ '%2$s',
+ $hidden_wrapper_attributes,
+ $label
+ );
+ }
}
remove_filter( 'next_posts_link_attributes', $filter_link_attributes );
} elseif ( ! $max_page || $max_page > $page ) {
- $custom_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) );
- if ( (int) $custom_query->max_num_pages !== $page ) {
- $content = sprintf(
- '%3$s',
- esc_url( add_query_arg( $page_key, $page + 1 ) ),
- $wrapper_attributes,
- $label
- );
+ $custom_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) );
+ $max_num_pages = $custom_query->max_num_pages ? $custom_query->max_num_pages : 1;
+ // If there are pages to paginate.
+ if ( 1 < $max_num_pages ) {
+ if ( (int) $max_num_pages !== $page ) { // If we are NOT in the last one.
+ $content = sprintf(
+ '%3$s',
+ esc_url( add_query_arg( $page_key, $page + 1 ) ),
+ $wrapper_attributes,
+ $label
+ );
+ } else { // If we are in the last one.
+ $content = sprintf(
+ '%2$s',
+ $hidden_wrapper_attributes,
+ $label
+ );
+ }
}
wp_reset_postdata(); // Restore original Post Data.
}
diff --git a/packages/block-library/src/query-pagination-previous/index.php b/packages/block-library/src/query-pagination-previous/index.php
index a31253bb25368..467d2f3932bf0 100644
--- a/packages/block-library/src/query-pagination-previous/index.php
+++ b/packages/block-library/src/query-pagination-previous/index.php
@@ -25,6 +25,7 @@ function render_block_core_query_pagination_previous( $attributes, $content, $bl
$default_label = __( 'Previous Page' );
$label = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? $attributes['label'] : $default_label;
$pagination_arrow = get_query_pagination_arrow( $block, false );
+ $content = '';
if ( $pagination_arrow ) {
$label = $pagination_arrow . $label;
@@ -39,29 +40,40 @@ function render_block_core_query_pagination_previous( $attributes, $content, $bl
};
add_filter( 'previous_posts_link_attributes', $filter_link_attributes );
- if ( 1 !== $paged ) {
- $content = get_previous_posts_link( $label );
- } else {
- // Prints the link only if there are pages to paginate.
- $content = $max_page > 1 ? sprintf(
- '%2$s',
- $hidden_wrapper_attributes,
- $label
- ) : '';
+
+ // If there are pages to paginate...
+ if ( 1 < $max_page ) {
+ if ( 1 !== $paged ) { // ... and we are NOT in the first one.
+ $content = get_previous_posts_link( $label );
+ } else { // ... and we are in the first one.
+ $content = sprintf(
+ '%2$s',
+ $hidden_wrapper_attributes,
+ $label
+ );
+ }
}
remove_filter( 'previous_posts_link_attributes', $filter_link_attributes );
- } else {
- $content = ( 1 !== $page ) ? sprintf(
- '%3$s',
- esc_url( add_query_arg( $page_key, $page - 1 ) ),
- $wrapper_attributes,
- $label
- ) : sprintf(
- '%2$s',
- $hidden_wrapper_attributes,
- $label
- );
-
+ } elseif ( ! $max_page || $max_page > $page ) {
+ $custom_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) );
+ $max_num_pages = $custom_query->max_num_pages ? $custom_query->max_num_pages : 1;
+ // If there are pages to paginate...
+ if ( 1 < $max_num_pages ) {
+ if ( 1 !== $page ) { // ... and we are NOT in the first one.
+ $content = sprintf(
+ '%3$s',
+ esc_url( add_query_arg( $page_key, $page - 1 ) ),
+ $wrapper_attributes,
+ $label
+ );
+ } else { // ... and we are in the first one.
+ $content = sprintf(
+ '%2$s',
+ $hidden_wrapper_attributes,
+ $label
+ );
+ }
+ }
}
return $content;
}