Pagination with wp_pagenavi not working on custom page

I am having trouble with wp_pagenavi working on the start page using a custom query. It's working well on every standard template like category.php. But not on the homepage.

Here is the code:

?php

        $args=query_posts(array(
          'post__not_in'= array(419),
          'post_type' = 'post',
          'posts_per_page' = 10,
          'paged' = get_query_var('page'), ));

        $my_query = new WP_Query($args);
        if( $my_query-have_posts() ) {

          while ($my_query-have_posts()) : $my_query-the_post(); ?
    ?php 
       $featuredImage = wp_get_attachment_url( get_post_thumbnail_id($post-ID) );

        $s1=strtolower($title);

       ?
    div id="post-?php echo $post-ID;?" ?php $item_format = is_video() ? 'video' : 'post'; post_class('item cf item-'.$item_format); ?
        div class="thumb"
        ?PHP 'a class="clip-link" data-id="'.$post-Id.'" title="'.esc_attr(get_the_title($post-Id)).'" href="'.get_permalink($post-Id).'"'?
             a class="clip-link"  title="?php the_title();?" href="?php the_permalink();?" rel="bookmark"
                span class="clip"
                    ?php echo'img src="'.$featuredImage.'" alt="'.esc_attr(get_the_title($post-Id)).'" /';?
                    span class="vertical-align"/span 
                /span

                span class="overlay"/span
            /a
        /div!--- thumb-----
    /div!-- end #post-?php the_ID(); ? --

    ?php

     endwhile;
     global $my_query;
     $total_pages = $my_query-max_num_pages; 

         if($total_pages  1)
         { ?
                div class="loop-nav pag-nav"
                div class="loop-nav-inner"
                    ?php 
                    if(function_exists('wp_pagenavi')) {
                        wp_pagenavi();
                    } else {
                        $label = __('laquo; Prev', 'dp');
                        if($prev = get_previous_posts_link($label))
                            echo str_replace('a', 'a clas="prev"', $prev);
                        else
                            echo 'span class="prev"'.$label.'/span';

                        $label = __('Next raquo;', 'dp');
                        if($next = get_next_posts_link($label))
                            echo str_replace('a', 'a class="next"', $next);
                        else
                            echo 'span class="next"'.$label.'/span';
                    } ?
                /div
                /div!-- end .loop-nav --
    ?php
 } 
    }

      wp_reset_query();  // Restore global post data stomped by the_post().
    ?
            /div!---nag cf----
            /div!---loop-content grid-mini---

The pagination shows up, but the url /page/2/ is not working, it shows /page/1/ result.

Topic plugin-wp-pagenavi pagination Wordpress

Category Web


Your construction of your custom loop is a bit of a mess unfortuantely. I'm not going to go through this now, but I'll add links for you to go and read through :-).

You should go and have a look on how to properly construct a custom query with WP_Query. You should also have a read about using the next_posts_link when using a custom query. The last point here, there are a few bugs in your code which you need to take a look at. You should set you debug to true in wp-config.php. Here is an article about Debugging Wordpress

At this point in time, you are only creating a custom query to remove one post from the loop. Apart from that, you are running the main query in a custom loop.

Here is how I would tackle this problem.

Reset your page with the standard loop, the same loop you've used that said is working on category.php. Now, use pre_get_posts to exclude your post from the main query. Here is the code. Add this in functions.php. This is modified from the codex.

function exclude_single_posts_home($query) {
    if ($query->is_home() AND $query- >is_main_query()) {
         $query->set('post__not_in', array(419));
    }
}

add_action('pre_get_posts', 'exclude_single_posts_home');

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.