0

I'm using Presso theme, if I click on the numbers in homepage to go to page 2 for example, my url becomes page/2/ and the highlighted page number is 2 but the page keeps showing the posts of the page 1. Same story for any other page number, clicked from the homepage. The index.php is

<?php get_header(); ?>

<div id="page-wrapper" class="container">
<?php echo do_shortcode( vw_get_option( 'header_ads_code' ) ) ; ?>
    <div class="row">
        <div id="page-content" class="col-sm-7 col-md-8">
            <?php $the_query = new WP_Query( 'cat=-7,-3' ); ?>
            <?php if ($the_query->have_posts()) : ?>

                <?php get_template_part( 'templates/page-title' ); ?>

                <?php $blog_layout = vw_get_option( 'blog_layout' ); ?>

                <?php if ( 'classic' == $blog_layout ) : ?>

                <div class="row archive-posts post-box-list">


                    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
                    <div class="col-sm-12 post-box-wrapper">
                        <?php get_template_part( 'templates/post-box/classic', get_post_format() ); ?>
                    </div>
                    <?php 
endwhile;



?>
                </div>

                <?php else: ?>

                <div class="row archive-posts vw-isotope post-box-list">
                    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
                    <div class="col-sm-6 post-box-wrapper">
                        <?php get_template_part( 'templates/post-box/large-thumbnail', get_post_format() ); ?>
                    </div>
                    <?php endwhile; ?>
                </div>

                <?php endif; ?>

                <?php get_template_part( 'templates/pagination' ); ?>

                <?php comments_template(); ?>

            <?php else : ?>

                <h2><?php _e('Sorry, no posts were found', 'envirra') ?></h2>

            <?php endif; ?>

        </div>

        <aside id="page-sidebar" class="sidebar-wrapper col-sm-5 col-md-4">
            <?php get_sidebar(); ?>
        </aside>
    </div>
</div>

<?php get_footer(); ?>

Digging in the teme, the pagination.php called by get_template_part is:

<?php global $wp_query, $wp_rewrite; ?>

<?php if ( $wp_query->max_num_pages > 1 ) : ?>
    <div id="pagination" class="header-font clearfix">
        <?php

            $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
            $pagination = array(
                'base' => @add_query_arg( 'paged','%#%' ),
                'format' => '',
                'total' => $wp_query->max_num_pages,
                'current' => $current,
                'prev_text' => __( '&laquo;', 'envirra' ),
                'next_text' => __( '&raquo;', 'envirra' ),
                'type' => 'plain'
            );

            if( $wp_rewrite->using_permalinks() )
                $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' ); 

            if( !empty( $wp_query->query_vars['s'] ) )
                $pagination['add_args'] = array( 's' => urlencode( get_query_var( 's' ) ) );

            // echo '<span>' . sprintf( __( 'Page&nbsp;&nbsp;%s&nbsp;&nbsp;of&nbsp;&nbsp;%s', 'envirra' ), $current, $wp_query->max_num_pages ) . '</span>'.' ';
            echo paginate_links( $pagination );

            if ( false ) posts_nav_link();
        ?>
    </div>
<?php endif; ?>

any help is highly appreciated

  • Sidenote: Using the @ operator at @add_query_arg is extremely bad practice, and doesn't do what you think it does – Tom J Nowell Nov 8 '15 at 4:39
  • Can we see the entire file? Your description implies you're calling query_posts or doing something else to create a new post loop, but I see no evidence of that in the code you posted. What are you doing to manipulate or replace your query? – Tom J Nowell Nov 8 '15 at 4:40
  • Also what are you doing to $pagination['base']? What kind of URLs are you trying to generate and why? – Tom J Nowell Nov 8 '15 at 4:41
  • @TomJNowell I included the index.php code in the question, as you can see it calls the get_template_part method to get the pagination template. – mante Nov 8 '15 at 10:54
  • Do not use the word Wordpress in the title, this site is related to Wordpress – Pieter Goosen Nov 8 '15 at 11:07
0

<?php $the_query = new WP_Query( 'cat=-7,-3' ); ?> is the cause of your problems, WordPress has taken care of the pagination, then you've thrown it away and not told your new query anything about the page you're currently on, so it defaults to the first.

It's equivalent to asking someone for a cup of tea, waiting 20 minutes for the kettle to boil, the tea to brew, then finally saying "I never wanted tea at all, go make me coffee"

As a result, WordPress is doing twice as much work, slowing your page down. You're also going to face issues when plugins try to modify the main query for functionality, only to find you never use it.

If you want to modify what posts WordPress fetches from the database, you should modify the main query before it's ran, rather than discarding it afterwards. You do this by using the pre_get_posts filter, e.g.:

// show only 1 category on the homepage
function my_home_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '123' );
    }
}
add_action( 'pre_get_posts', 'my_home_category' );

You now have everything you need to re-implement your code to not need the WP_Query, refer to the links at the end of this answer for more information that may be helpful ( I strongly recommend you read them, it will save you a lot of time and hassle )

I'd also recommend you do not use index.php as your homepage, it's the main fallback template file, and should act as a safety net/catch all for when WordPress doesn't know which template to load. Instead consider using home.php

Further Reading

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.