0

I currently have simple pagination set up showing the text older and newer. If one of the text isn't needed it display an 'inactive' version of it so that the text doesn't disappear.

<?php if( get_next_posts_link() ) :?>
                <div id="next-post-link" class="post-pag flL"><?php next_posts_link( '<i class="icon-left-open-big"></i><span>Older</span>' );?></div>
            <?php else: ?>
                <div id="next-post-link" class="post-pag inactive flL"><i class="icon-left-open-big"></i><span>Older</span></div>
            <?php endif; ?>

            <?php if( get_previous_posts_link() ) :?>
                <div id="prev-post-link" class="post-pag flR"><?php previous_posts_link( '<span>Newer</span><i class="icon-right-open-big"></i>' );?></div>
            <?php else: ?>
                <div id="prev-post-link" class="post-pag inactive flR"><span>Newer</span><i class="icon-right-open-big"></i></div>
            <?php endif; ?>

This is working fine however I'm struggling to achieve a page counter. I've looked at several questions about numbered pagination but it seems over the top for what I want. I'm looking to print the current page that I'm on and print the total amount of pages all the time.

This image should show what I mean better: enter image description here

This is my Query so far:

$current_page = get_query_var( 'paged' );
    $pages = $wp_query->max_num_pages;

    $args = array(
        'paged'=> $paged,
        'posts_per_page' => '2'
    );

    $wp_query = new WP_Query($args); 

    if ($wp_query->have_posts()) : 
0

This post on the WordPress forums should provide some assistance. It uses the paginate_links function to generate the output.

0

For any one struggling with this there is an answer here on SO. As I am going to use this in multiple places I used the function answer.

Add to be functions.php:

function current_paged( $var = '' ) {
if( empty( $var ) ) {
    global $wp_query;
    if( !isset( $wp_query->max_num_pages ) )
        return;
    $pages = $wp_query->max_num_pages;
}
else {
    global $$var;
        if( !is_a( $$var, 'WP_Query' ) )
            return;
    if( !isset( $$var->max_num_pages ) || !isset( $$var ) )
        return;
    $pages = absint( $$var->max_num_pages );
}
if( $pages < 1 )
    return;
$page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
echo 'Page ' . $page . ' of ' . $pages;
}

Call the function on your page you want:

<?php current_paged(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.