• Resolved ecabral

    (@ecabral)


    Hi there!

    I’m redesigning my website, originally created with a classic theme and a popular website builder, using only the block editor and twenty twenty-four theme. The learning curve has been steep, so I’m likely missing something here…

    I’m using the query loop to display posts. I’ve played around with pretty much all patterns and am confused with the following: grid and offset patterns (ie, patterns displaying the posts in multiple columns) only seem to support pagination per post template, rather than per pattern. Take a look at these setups:

    Because there are two query loops per column here, there are also two post templates and hence two pagination blocks (!). My desired behavior would be one pagination block per pattern.

    I also noticed that the pagination URLs of grid patterns in general result in ugly URLs such as: /?query-321-page=2 , /?query-321-page=3, /?query-322-page=2, etc. One column patterns result in familiar pagination URLs such as /page/2/, /page/3/, etc.

    I read the documentation, namely:

    Pagination block is included by default if you choose a correct pattern in a Query Loop block. That said, you can also insert it manually when needed.

    What is a “correct pattern”? How can I manually insert the pagination block for grid and offset patterns?

    What about the pagination URLs – is there a workaround here?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Issue 1: Multiple Pagination Blocks

    Grid and offset patterns create multiple Query Loops, each with its own pagination block.

    Solution:
    Use a single Query Loop with a grid layout and configure the “Offset” setting for layout control. Add one Pagination block within this Query Loop.

    Issue 2: Ugly Pagination URLs

    Grid patterns generate URLs like /?query-321-page=2 instead of /page/2/.

    Solution:
    Consolidate Query Loops to a single instance. Alternatively, add custom rewrite rules in PHP to clean up the URLs.

    Consolidating Query Loop

    Ensure you use only one Query Loop block in the block editor. For grid layouts, avoid separate loops for each column.

    <!-- Example PHP for Query Loop in Template -->
    $args = array(
    'posts_per_page' => 6, // Total posts to display
    'paged' => get_query_var('paged', 1),
    );
    $query = new WP_Query($args);

    if ($query->have_posts()) : ?>
    <div class="posts-grid">
    <?php while ($query->have_posts()) : $query->the_post(); ?>
    <div class="post-item">
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <p><?php the_excerpt(); ?></p>
    </div>
    <?php endwhile; ?>
    </div>
    <div class="pagination">
    <?php
    echo paginate_links(array(
    'total' => $query->max_num_pages,
    ));
    ?>
    </div>
    <?php endif; wp_reset_postdata();

    Rewrite Rules for Clean URLs

    Add this to your theme’s functions.php file to ensure clean pagination URLs:

    add_action('init', function() {
    add_rewrite_rule(
    '^page/([0-9]+)/?$',
    'index.php?pagename=your-page-slug&paged=$matches[1]',
    'top'
    );
    });

    Note: Replace your-page-slug with the actual page slug. This will clean up pagination URLs while consolidating the Query Loop ensures a single pagination block.

    Thread Starter ecabral

    (@ecabral)

    Hi there, @95dubai !

    Regarding issue #1 – mind that I’m not trying to offset the posts (or skipping x number of posts, which is what that setting you mention does). By the way, that setting is pretty hidden, but fortunately this helped!

    What I’m trying to understand is how to add pagination to at least two query loop layout patterns. These happen to be provided by twenty twenty-four theme. The names I provided in my original post are the exact names of the patterns used by the theme. Coincidentally, one of them is called “offset” as the setting you mention.

    In short, these are the only two patterns that offer an asymmetrical magazine-like style and I’d expect them to support pagination.

    I’m realizing now that maybe I should have probably posted in the theme support forum? At the time of posting this seemed like a query loop-specific question, but maybe this is theme specific?

    Thread Starter ecabral

    (@ecabral)

    For the record, this question was answered here.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.