Ross Coulbeck
Ross Coulbeck

Reputation: 612

Getting page numbers while paginating wordpress posts page

I'm trying to create some navigation with pictures of circles side by side for a posts page in Wordpress. Basically when it's the active page it needs to be a white circle and when it's not it needs to be a darker circle.

I've got them all to display darker circles easy enough but am struggling with getting the active page. Simply changing the a:active css doesn't work.

Is there a way to get the page number while paginating so I can give the links a different class e.g. activeCircle, when it is the active page. I've got the rest of the code figured out, just need an accurate way of getting the page I'm on.

Have tried $_GET from the super global and $page so far which I've seen other people use. Neither seem to output anything.

Code:

<div id="nav_numbers">
        <?php

        for($i = 1; $i <= (ceil(wp_count_posts()->publish / get_option('posts_per_page'))); $i++){

            $class = ($page == $i)?'class="activeDot"':'';

            ?>
            <a href="<?php get_site_url(); ?> /news/latest-news/page/<?= $i;?>/" <?=$class;?> >&nbsp;</a>
            <?php
        } ?>
</div>

The $page is the bit that doesn't work.

Upvotes: 1

Views: 2351

Answers (1)

seferov
seferov

Reputation: 4161

A little bit modification:

<?php
$url = $_SERVER["REQUEST_URI"];
$segments = explode('/', $url);
$current_page_number = end($segments);

<div id="nav_numbers">
    <?php for($i = 1; $i <= (ceil(wp_count_posts()->publish / get_option('posts_per_page'))); $i++) : ?>
        <?php $class = if($current_page_number == $i ? 'active' : '' ) ?>
        <a href="<?php get_site_url(); ?>/news/latest-news/page/<?=$i?>/" class="<?=$class?>">&nbsp;</a>
    <?php endfor; ?>
</div>

Upvotes: 1

Related Questions