Pagenavi Plugin and Custom Post Type Navigation Error (404)


Staff member
I have a similar problem with this ( <a href=""></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 I use WP_Pagenavi plugin. Whenever I try to go to page 2 or higher, the url becomes 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

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


        $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;

            wp_pagenavi( );

and here is my functions.php code for the gallery

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 !