Pagenavi Plugin and Custom Post Type Navigation Error (404)

admin

Administrator
Staff member
I have a similar problem with this ( <a href="https://wordpress.stackexchange.com/questions/9593/custom-post-type-archive-with-pagination">https://wordpress.stackexchange.com/questions/9593/custom-post-type-archive-with-pagination</a> ) and can't really figure out what to do !

I have a custom post type name 'gallery' and I have created a template to show all 'gallery' items. The url is www.domain.com/gallery. I use WP_Pagenavi plugin. Whenever I try to go to page 2 or higher, the url becomes www.domain.com/gallery/page/2 and it returns a 404 page. I read everywhere about it and I guess it has something to do with rewrite rules, the query and whatever else!

I have tried adding

Code:
add_rewrite_rule( 'gallery/page/([0-9]+)/?$', 'index.php?pagename=gallery&amp;paged=$matches[1]', 'top' );

The thing is that I don't want to change my permalinks structure which now is /%postname%/.

Here is my full code

Code:
        &lt;?php

        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $args = array(
            'post_type' =&gt; 'gallery',
            'paged' =&gt; $paged,
            'showposts' =&gt;24
            ); query_posts($args);

        if ( have_posts() ) : while ( have_posts() ) : the_post(); ?&gt;


                ...stuff here...

                &lt;?php endwhile; endif; ?&gt;


        &lt;?php 
            wp_pagenavi( );
        ?&gt;

and here is my functions.php code for the gallery

Code:
add_action('init', gallery);
function gallery() {
$args = array(
        'label' =&gt; __('Gallery'),
        'singular_label' =&gt; __(Gallery),
        'public' =&gt; true,
        'show_ui' =&gt; true,
        'capability_type' =&gt; 'post',
        'hierarchical' =&gt; false,
        'rewrite' =&gt; true,
        'supports' =&gt; array('title', 'editor', 'thumbnail', 'custom-fields')        
);

register_post_type( 'gallery' , $args );
add_rewrite_rule( 'gallery/page/([0-9]+)/?$', 'index.php?pagename=gallery&amp;paged=$matches[1]', 'top' );
}

I am pulling my hair with this (grrrrr).
Thank you in advance !