Z
Z
Zwed2018-04-19 19:58:32
HTML
Zwed, 2018-04-19 19:58:32

What's wrong with Wordpress pagination?

I'm trying to make pagination using ready-made code, everything seems to work out, and pagination is visible and pages switch, and the address bar changes. The posts themselves don't change. Regardless of the page, it shows the last post.
Please tell me how to fix it.
Thanks in advance!
Post output code:

<article id="news_posts">
        <?php if ( have_posts() ) : ?>
        <?php $args = array('category' => 4, 'posts_per_page' => 1);
        $myposts = get_posts( $args );
        foreach( $myposts as $post ){ setup_postdata($post); ?>
          <div class="row new_article">
            <div class="col-xs-12 col-sm-4 col-xl-3 no-gutters">
              <?php if ( has_post_thumbnail() ) : ?>
                <a href="<?php the_permalink( $post ); ?>"><img class="post_thumbnails media-object" src="<?php the_post_thumbnail_url(); ?>" alt="Olaines 1.vidusskola - jaunumi"></a>
              <?php endif; ?>
            </div>
            <div class="col-xs-10 col-xs-push-1 col-sm-8 col-sm-push-0 col-xl-9">
              <h1 class="post_heading"><a href="<?php the_permalink( $post ); ?>"><?php the_title();?></a></h1>
              </br>
              <div class="excerpt">
                <?php the_excerpt(); ?>
              </div>
              <a href="<?php the_permalink( $post ); ?>"><button type="button" class="btn btn-primary">Lasīt tālāk <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span></button></a>
            </div>
          </div>
        <?php
        }
        wp_reset_postdata();
        else: ?>
          <p>Piedodied, netika atrasti ieraksti, kas atbilstu jūsu kritērijiem</p>
        <?php endif; ?>
      </article>

Pagination code from index.php: Code in functions.php:
<?php wpo1vsk_pagination(); ?>
/* WordPress Bootstrap Pagination*/
function wpo1vsk_pagination( $args = array() ) {

    $defaults = array(
        'range'           => 4,
        'custom_query'    => FALSE,
        'previous_string' => __( 'Iepriekšējā', 'text-domain' ),
        'next_string'     => __( 'Nākošā', 'text-domain' ),
        'before_output'   => '<div class="page-numbers"><ul class="pagination">',
        'after_output'    => '</ul></div>'
    );

    $args = wp_parse_args(
        $args,
        apply_filters( 'wpo1vsk_pagination', $defaults )
    );

    $args['range'] = (int) $args['range'] - 1;
    if ( !$args['custom_query'] )
        $args['custom_query'] = @$GLOBALS['wp_query'];
    $count = (int) $args['custom_query']->max_num_pages;
    $page  = intval( get_query_var( 'paged' ) );
    $ceil  = ceil( $args['range'] / 2 );

    if ( $count <= 1 )
        return FALSE;

    if ( !$page )
        $page = 1;

    if ( $count > $args['range'] ) {
        if ( $page <= $args['range'] ) {
            $min = 1;
            $max = $args['range'] + 1;
        } elseif ( $page >= ($count - $ceil) ) {
            $min = $count - $args['range'];
            $max = $count;
        } elseif ( $page >= $args['range'] && $page < ($count - $ceil) ) {
            $min = $page - $ceil;
            $max = $page + $ceil;
        }
    } else {
        $min = 1;
        $max = $count;
    }

    $echo = '';
    $previous = intval($page) - 1;
    $previous = esc_attr( get_pagenum_link($previous) );

    $firstpage = esc_attr( get_pagenum_link(1) );
    if ( $firstpage && (1 != $page) )
        $echo .= '<li class="previous"><a href="' . $firstpage . '" class="page-link">' . __( 'Pirmā', 'text-domain' ) . '</a></li>';
    if ( $previous && (1 != $page) )
        $echo .= '<li><a href="' . $previous . '" class="page-link" title="' . __( 'iepreikšējā', 'text-domain') . '">' . $args['previous_string'] . '</a></li>';

    if ( !empty($min) && !empty($max) ) {
        for( $i = $min; $i <= $max; $i++ ) {
            if ($page == $i) {
                $echo .= '<li class="active"><span class="page-link current">' . str_pad( (int)$i, 2, '0', STR_PAD_LEFT ) . '</span></li>';
            } else {
                $echo .= sprintf( '<li><a href="%s" class="page-link">%002d</a></li>', esc_attr( get_pagenum_link($i) ), $i );
            }
        }
    }

    $next = intval($page) + 1;
    $next = esc_attr( get_pagenum_link($next) );
    if ($next && ($count != $page) )
        $echo .= '<li><a href="' . $next . '" class="page-link" title="' . __( 'next', 'text-domain') . '">' . $args['next_string'] . '</a></li>';

    $lastpage = esc_attr( get_pagenum_link($count) );
    if ( $lastpage ) {
        $echo .= '<li class="next"><a href="' . $lastpage . '" class="page-link">' . __( 'Pēdējā', 'text-domain' ) . '</a></li>';
    }
    if ( isset($echo) )
        echo $args['before_output'] . $echo . $args['after_output'];
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan Hasanli, 2018-04-20
@azerphoenix

And I use this pagination - dimox.name/wordpress-pagination-without-a-plugin Implementation
example - https://md7.info/fakty

B
branky, 2018-08-24
@branky

To use pagination with Bootstrap 4 , take a ready-made and proven feature: WordPress Bootstrap Pagination

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question