Last updated on October 18, 2023

Divi blog post pagination

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Add pagination to blog posts in Divi.

The code snippet provided below demonstrates how to implement Divi blog post pagination in WordPress. This functionality is useful when you want to split long blog posts into multiple pages to improve user experience and make it easier for readers to navigate through the content.

function wpsnippets_divi_blog_pagination() {
    global $paged, $wp_query;

    // Get the total number of pages
    $total_pages = $wp_query->max_num_pages;

    // Check if there is more than one page
    if ($total_pages > 1) {
        // Get the current page number
        $current_page = max(1, $paged);

        // Output the pagination markup
        echo '<div class="pagination">';
        echo paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => 'page/%#%',
            'current' => $current_page,
            'total' => $total_pages,
            'prev_text' => __('&laquo; Previous', 'textdomain'),
            'next_text' => __('Next &raquo;', 'textdomain'),
        ));
        echo '</div>';
    }
}

To use this code snippet, you can add it to your theme’s functions.php file or create a custom plugin. Once added, you can call the wpsnippets_divi_blog_pagination() function in your theme template files, such as archive.php or index.php, where you want to display the pagination.

The code first checks if there is more than one page by comparing the total number of pages with 1. If there is more than one page, it retrieves the current page number and outputs the pagination markup using the paginate_links() function. The paginate_links() function generates the HTML markup for the pagination, including the previous and next links.

You can customize the pagination markup and styling by modifying the HTML output within the echo statements. Additionally, you can adjust the text for the previous and next links by changing the values of the 'prev_text' and 'next_text' parameters in the paginate_links() function.

Examples

Example 1: Adding Divi blog post pagination to a custom WordPress query

This example demonstrates how to add Divi blog post pagination to a custom WordPress query. The code uses the wpsnippets_get_pagination() function to generate the pagination links.

<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'post_type'      => 'post',
    'posts_per_page' => 5,
    'paged'          => $paged,
);

$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // Display post content
    }

    echo wpsnippets_get_pagination( $query );
}

wp_reset_postdata();
?>

The code sets up a custom WordPress query with the desired parameters, including the paged parameter to handle pagination. It then loops through the posts and displays their content. Finally, it calls the wpsnippets_get_pagination() function to generate the pagination links based on the query.

Example 2: Adding Divi blog post pagination to the main WordPress loop

This example shows how to add Divi blog post pagination to the main WordPress loop. The code uses the wpsnippets_get_pagination() function to generate the pagination links.

<?php
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        // Display post content
    }

    echo wpsnippets_get_pagination();
}
?>

The code checks if there are any posts in the main WordPress loop and loops through them, displaying their content. It then calls the wpsnippets_get_pagination() function to generate the pagination links for the main loop.

Example 3: Customizing Divi blog post pagination output

This example demonstrates how to customize the output of Divi blog post pagination. The code uses the wpsnippets_get_pagination() function with custom arguments to modify the pagination links.

<?php
$args = array(
    'prev_text' => 'Previous',
    'next_text' => 'Next',
);

echo wpsnippets_get_pagination( null, $args );
?>

The code calls the wpsnippets_get_pagination() function with custom arguments to modify the pagination links. In this example, the “Previous” and “Next” text for the pagination links are customized.

Last updated on October 18, 2023. Originally posted on December 26, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *