1

I have index.php with a pagination. The permalink settings is plain permalinks.

I have a correct pagination view when I open a page /?paged=2 but if I open just index.php I have all posts without pagination. There is my index.php:

<?php get_header(); ?>

<?php

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

$posts_per_page = get_option('posts_per_page');

$args = array('post__in' => get_option( 'sticky_posts' 
),'posts_per_page' => $posts_per_page, 'paged' => $paged);

$the_query = new WP_Query( $args);


if ( $the_query->have_posts() ) : ?>


<div class="section">
<div class="container">
    <div class="wrap-content">
        <div class="pg-block list">
        <?php
            while ( $the_query->have_posts() ) : $the_query->the_post();
                get_template_part( 'content', get_post_format() );
            endwhile;
        ?>
        </div>

        <?php wp_reset_postdata(); ?>

        <aside class="wrap-aside hid-xs">
            <div class="block-inner">
            <?php dynamic_sidebar('sidebar-1');?>
            </div>
        </aside>
    </div>
    <div class="wrap-content">
        <?php
        //number of pages for pagination
        $total_pages =  $the_query->found_posts / $posts_per_page;

        if ($the_query->found_posts % $posts_per_page != 0) $total_pages++;
            $args_nav = array(
            'show_all'     => false, 
            'end_size'     => 2,     
            'mid_size'     => 2,     
            'prev_next'    => True,  
            'prev_text'    => '',    
            'next_text'    => '',    
            'total'        => $total_pages, 
            'add_args'     => False,
            'add_fragment' => '',     
            'screen_reader_text' =>  ' '
        );
            the_posts_pagination($args_nav);
        ?>
    </div>
</div>
</div>


<?php else : ?>
<p><?php _e( 'Posts not found' ); ?></p>
<?php endif;

get_footer();?>

PS: I just edit my code like that but nothing was changed. The first page displays all posts without pagination

<?php if ( have_posts() ) : ?>

<div class="section">
<div class="container">
    <div class="wrap-content">
        <div class="pg-block list">
        <?php
            while ( have_posts() ) : the_post();
                get_template_part( 'content', get_post_format() );
            endwhile;
        ?>
        </div>

        <?php wp_reset_postdata(); ?>
  • I'm not entirely sure what you want to do, but two things. Do not change the main query for a custom one on the home page. Use pre_get_posts to alter the main query. Also, the_posts_pagination() only works with the main query, it doesnot work with custom queries, except if you hack the main query – Pieter Goosen Dec 20 '15 at 5:10
  • @PieterGoosen : I just edited my code like in P.S. but nothing was changed. – Sergei Rodionov Dec 20 '15 at 16:16
0

Adding ignore sticky line inside array should solve the issue :)

$args = array(
       'post_type'      => 'post',
       'posts_per_page' => '6',
       'ignore_sticky_posts' => 1,//this is the one :)
       'paged' => $paged,           
);
  • It is always worth adding a few words of explanation. – nmr Aug 24 '18 at 8:31
  • Added explanation while posting I didn't notice it got removed when I was editing – Varsha Dhadge Aug 24 '18 at 8:59
-1

Add this two lines to your theme's function.php file and everything will get back to work:

add_filter(‘redirect_canonical’,’custom_disable_redirect_canonical’);

function custom_disable_redirect_canonical($redirect_url) {if (is_paged() && is_singular()) $redirect_url = false; return $redirect_url; }
  • Please clean up your code, it is unreadable as everything is in one lllloooonnngggg line – Pieter Goosen Jan 19 '16 at 6:06

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.