• Resolved Cedrass

    (@cedrass)


    For reasons I won’t go in details into, I have had to create custom posts for our website and now I am having trouble getting the pagination to work on the archives pages for each new post type.

    I can have the page display only the first 6 (number I chose) items, but clicking on “Next” leads me to a “Page not found” page.

    Here’s how I created the new post types:

    function createPostType( $slug, $singularSlug, $name, $singular, $allowComments = true )
    {
        register_post_type( $slug,
            array(
                'labels' => array(
                    'name' => __( $name ),
                    'singular_name' => __( $singular ),
                    'menu_name' => __( $slug )
                ),
                'public' => true,
                'has_archive' => true,
                "show_in_menu" => true,
                "show_in_nav_menus" => true,
                "taxonomies" => array("category"),
                "supports" => array( "title", "editor", "author", "thumbnail", "excerpt" )
            )
        );
    
        if( $allowComments )
            add_post_type_support( $slug, array( 'comments' ) );
    }
    
    function createNewPostTypes()
    {
        createPostType( "themes", "theme", "Thèmes", "Thème", false );
        createPostType( "articles", "article", "Articles", "Article" );
        createPostType( "galleries", "gallerie", "Galeries", "Galeries" );
        createPostType( "videos", "video", "Vidéos", "Vidéo" );
    }

    and this is the archive’s template:

    <?php
    get_header();
    ?>
    
        <div id="primary" class="site-content dual-column-layout">
            <div id="content" role="main">
                <?php
                $temp = $wp_query;
                $wp_query = null;
                $wp_query = new WP_Query();
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                $wp_query->query('showposts=6&post_type=articles'.'&paged='.$paged);
    
                $counter = 0;
                while ($wp_query->have_posts()) : $wp_query->the_post();
    
                    if( $counter == 0 )
                        echo "<div style='clear: both'>";
    
                    echo "<a href='".get_permalink()."'>";
                        echo "<div class=\"post-list-thumbnail\">";
                            generateThumbnail( ThemeThumbnailRenderer );
                        echo "</div>";
                    echo "</a>";
    
                    $counter++;
                    if( $counter == 2 )
                    {
                        echo "</div>";
                        $counter = 0;
                    }
    
                    if( ( $wp_query->current_post + 1 ) == ( $wp_query->post_count ) && $counter != 0 )
                        echo "</div>";
    
                endwhile;
                ?>
    
                <nav>
                    <?php previous_posts_link('« Newer') ?>
                    <?php next_posts_link('Older »') ?>
                </nav>
    
                <?php
                $wp_query = null;
                $wp_query = $temp;  // Reset
                ?>
    
            </div>
        </div>
    
    <?php
        get_sidebar();
        get_footer();
    ?>

    I have tried the many solutions given by people on this forum, but nothing seems to work. I thought it might have been the URL rewrite problem, but it wasn’t (prior to the current configuration I’m using, I was using the Article Name options for URLs. I now reverted to default). Or at least it does not seem like it is.

    I am starting to wonder if a working pagination is something possible for custom post’s archives…

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Cedrass

    (@cedrass)

    Two new things I tried:

    Putting this “die(print_r(“====”.$paged, true ));” right after I set the $paged variable, to see if I at least reached it in my second page. I got “====1” on my first page (which is what I intended), but the second page didn’t show anything. It went to the “Page not found” page. I’m not really sure what that behavior means. Is WordPress ignoring my archive-articles.php page because of the &paged=2 argument?

    Also, instead of
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;"
    I set it to 2
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 2;
    And surprise, it did show me what should be the second page.

    I’m really not getting why this isn’t working…

    Thread Starter Cedrass

    (@cedrass)

    To anyone who might find this topic in the future, searching for an answer, I fixed my problem. This post was what gave me the solution.

    Hopefully WP will fix this bug in a next version!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom post archive and pagination not working’ is closed to new replies.