paginate links – Add css class to Pagination?

Question

I’ve been using this code:

$current_page = get_query_var('paged');
$current_page = max( 1, $current_page );

$per_page = 12;
$offset_start = 1;
$offset = ( $current_page - 1 ) * $per_page + $offset_start;

$post_list = new WP_Query(array(
    'cat'            => -15,
    'posts_per_page' => $per_page,
    'paged'          => $current_page,
    'offset'         => $offset, // Starts with the second most recent post.
    'orderby'        => 'date',  // Makes sure the posts are sorted by date.
    'order'          => 'DESC',  // And that the most recent ones come first.
));

// Manually count the number of pages, because we used a custom OFFSET (i.e.
// other than 0), so we can't simply use $post_list->max_num_pages or even
// $post_list->found_posts without extra work/calculation.
$total_rows = max( 0, $post_list->found_posts - $offset_start );
$total_pages = ceil( $total_rows / $per_page );

if ( $post_list->have_posts() ):
    while ( $post_list->have_posts() ):
        $post_list->the_post();


        // loop output here
    endwhile;

    echo paginate_links( array(
        'total'   => $total_pages,
        'current' => $current_page,
    ) );
endif;
wp_reset_postdata();

This is from this answer: https://stackoverflow.com/questions/49227520/how-can-i-paginate-wp-query-results-when-theres-an-offset

Would it be possible to add a css class around this line?

   echo paginate_links( array(
        'total'   => $total_pages,
        'current' => $current_page,
    ) );

So it would be something like:

<div class="col-lg-12">
   echo paginate_links( array(
        'total'   => $total_pages,
        'current' => $current_page,
    ) );
</div>

Everything I’ve tried has led to a WordPress Error Page

Thanks!

in progress 0
Rebecca 1 month 2021-08-15T10:57:47-05:00 0 Answer 0 views 0

Answer ( 1 )

    0
    2021-08-15T23:33:10-05:00

    You can do what you are trying to do – add a CSS class selector to an HTML div tag, but you are mixing HTML and PHP code incorrectly. In order to do this correctly, you need to use the PHP opening/closing tags accordingly, such as:

    ?>
    <div class="col-lg-12">
    <?php
       echo paginate_links( array(
            'total'   => $total_pages,
            'current' => $current_page,
        ) );
    ?>
    </div>
    <?php
    

    Alternatively, you could also do it like this (no need for the PHP opening/closing tags):

    echo '<div class="col-lg-12">';
       echo paginate_links( array(
            'total'   => $total_pages,
            'current' => $current_page,
        ) );
    echo '</div>';
    

Leave an answer

Browse
Browse