Paging problems using Wp_query

Asked

Viewed 17 times

-1

Talk people, everything cool? I’m having problems with the WP pagination on the category pages of the site I’m developing. Suppose I want to display 06 posts per page, right? But instead of the site displaying 06 different posts each page, it displays the same posts on multiple pages.

Did someone have a similar problem and managed to solve it? Below, I share the code I’m using.

<!-- Global Page Section Start -->
<section class="global-page-header">
    <div class="container">
        <div class="row">
            <div class="col-lg-12">
                <div class="block">
                    <h2><?php the_category(''); ?></h2>
                    <ol class="breadcrumb">
                        <li>
                            <a href="index.php">
                                <i class="ion-ios-home"></i>
                                <?php the_breadcrumb(); ?>
                            </a>
                        </li>
                    </ol><!-- .breadcrumb -->

                    <div class="box-voltar">
                        <a href="javascript:history.back()" title="Voltar à Página Anterior" class="button-voltar"><i class="fa fa-chevron-circle-left"></i> Voltar</a>
                    </div><!-- .box-voltar -->
                </div><!-- .block -->
            </div><!-- .col-lg-12 -->
        </div><!-- .row -->
    </div><!-- .container -->  
</section><!--.global-page-header-->



<section class="main">
    <div class="container">
        <div class="row">
            <div class="col-lg-12 conteudo-interno">
                <?php
                //the arguments for loop
                $args = array(
                    'category_name'  => 'servicos',
                    'orderby'        => 'title',
                    'order'          => 'ASC',
                    'posts_per_page' => '1'
                ); 

                //the query
                $posts_servicos = new WP_Query($args); ?>

                <?php if ( $posts_servicos->have_posts()) : ?>

                <!-- the loop -->
                <?php while ( $posts_servicos->have_posts()) : $posts_servicos->the_post(); ?>

                    <div class="col-lg-4">
                        <div class="box-post text-center">
                            <div class="imagem-redonda">
                                <a href="<?php the_permalink(); ?>">
                                    <?php 
                                        if(has_post_thumbnail()){
                                            the_post_thumbnail('images');
                                        }else{
                                            echo '<img alt="<?the_title();?>" src="'.get_bloginfo("template_url").'/images/default.png" />';
                                        }
                                    ?>
                                </a>
                            </div><!--.imagem-redonda-->

                            <h2><?php the_title(); ?></h2>

                            <a href="<?php the_permalink(); ?>" title="Ler Mais">
                                <button type="button" class="btn btn-default btn-xs button-category">Saiba Mais</button>
                            </a>
                        </div><!--.box-post-->
                    </div><!-- .col-lg-4 -->

                <?php endwhile; ?>
                <!-- end of the loop -->

                <?php wp_reset_postdata(); ?>

                <?php else : ?>
                    <p><?php _e('<h1>Desculpe!</h1><p>Nenhum conteúdo cadastrado!</p>');?></p>
                <?php endif; ?>

                <div class="clear"></div>

                <div class="pagination">
                    <?php post_pagination(); ?>
                </div><!--.pagination-->

                <div class="clear"></div>
            </div><!-- .col-lg-12 .conteudo-interno-->
        </div><!-- .row -->
    </div><!-- .container -->
</section><!-- .main -->

1 answer

0

Not working properly because you forgot to enter the parameter paged. This parameter tells which page you are on, as it is not informed, on all pages it will show the data of the first page.

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'category_name'  => 'servicos',
    'orderby'        => 'title',
    'order'          => 'ASC',
    'posts_per_page' => '1',
    'paged'          => $paged
); 
  • Speak Kayo! Boy, I took a test and it worked perfidiously. Thank you.

  • Good that my reply helped you @rafaelpimentao if possible mark as best answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.