Aleksey Spirin
Aleksey Spirin

Reputation: 170

wordpress pagination doesn't work on "previous page"

I really get stuck with pagination problem in wordpress for more than a week. The problem: On the first blog page (basically when you just click on the nav menu "blog") There IS a pagination - here is the source http://advokat-belyakova.ru/blog/ by pagination I mean just next and prev buttons. But when I click "previous posts" ("« К предыдущим записям") right under the posts pagination. Pagination dissapears (http://advokat-belyakova.ru/blog/page/2/)

I'm using simple wp_query

<?php   $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $temp = $wp_query;$wp_query= null;
        $wp_query = new WP_Query(); $wp_query->query('showposts=20' . '&paged='. $paged . '&cat=-41');
        while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

I also tried without (this $paged I've found in stackoverflow but it doesn't help me at all :(

 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

Here is my code after the infromation i've got by wp_query loop

<?php endwhile; ?>
        <div class="row">
            <div class="col-md-12">
                <?php if ($paged > 1) { ?>

                    <nav id="nav-posts">
                        <div class="prev"><p class="pagination"><?php next_posts_link('&laquo; К предыдущим записям'); ?></p></div>
                        <div class="next"><p class="pagination pagination-next"><?php previous_posts_link('К новым записям &raquo;'); ?></p></div>
                    </nav>

                <?php } else { ?>

                    <nav id="nav-posts">
                        <div class="prev"><?php next_posts_link('&laquo; К предыдущим записям'); ?></div>
                    </nav>

                <?php } ?>

            </div>
        </div>
        <?php wp_reset_postdata(); ?>

I'm sorry for russian language in my web site examples. I hope it will be enough and you'll be able to understand what my problem is.

I have static main page template (custom) but i really haven't got this problem in the past. If additional info is necessary please ask for it

Upvotes: 1

Views: 286

Answers (1)

Aleksey Spirin
Aleksey Spirin

Reputation: 170

Find a function that works in my situation

Here is the function:

// Numbered Pagination
if ( !function_exists( 'wpex_pagination' ) ) {

function wpex_pagination() {

    $prev_arrow = is_rtl() ? '&rarr;' : '&larr;';
    $next_arrow = is_rtl() ? '&larr;' : '&rarr;';

    global $wp_query;
    $total = $wp_query->max_num_pages;
    $big = 999999999; // need an unlikely integer
    if( $total > 1 )  {
        if( !$current_page = get_query_var('paged') )
            $current_page = 1;
        if( get_option('permalink_structure') ) {
            $format = 'page/%#%/';
        } else {
            $format = '&paged=%#%';
        }
        echo paginate_links(array(
            'base'          => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format'        => $format,
            'current'       => max( 1, get_query_var('paged') ),
            'total'         => $total,
            'mid_size'      => 3,
            'type'          => 'list',
            'prev_text'     => $prev_arrow,
            'next_text'     => $next_arrow,
        ) );
    }
}

}

The wp_query in blog-page.php (actually almost the same):

<?php 
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $temp = $wp_query;$wp_query= null;
        $wp_query = new WP_Query(); $wp_query->query('showposts=7' . '&paged='. $paged . '&cat=-41');
        while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

After getting content inside the loop:

<?php endwhile; ?>
        <div class="row">
            <div class="col-md-12 pagination-wrapper">
                <?php wpex_pagination(); ?>


            </div>
        </div>
        <?php wp_reset_postdata(); ?>

Some basic css for displaying pagination

ul.page-numbers {
list-style: none;
margin: 0;
}

.page-numbers:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}

ul.page-numbers li {
display: block;
float: left;
margin: 0 4px 4px 0;
text-align: center;
}

.page-numbers a,
.page-numbers span {
line-height: 1.6em;
display: block;
padding: 0 6px;
height: 18px;
line-height: 18px;
font-size: 12px;
text-decoration: none;
font-weight: 400;
cursor: pointer;
border: 1px solid #ddd;
color: #888;
}

.page-numbers a span { padding: 0 }

.page-numbers a:hover,
.page-numbers.current,
.page-numbers.current:hover {
color: #000;
background: #f7f7f7;
text-decoration: none;
}

.page-numbers:hover { text-decoration: none }

Dont forget to say thanks to this guy. http://www.wpexplorer.com/pagination-wordpress-theme/

Upvotes: 0

Related Questions