I have a pagination that works perfectly fine on my homepage, the code goes:

    <div class="articles">
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $query = new WP_Query( array( 'post_type' => 'articles', 'orderby'=> 'menu_order', 'posts_per_page' => 5,'paged' => $paged  ) );

        if ( $query->have_posts() ) : ?>
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                <div class="article">
                    <div class="entry">
                        <h4 class="title oswald post-title"><?php the_title(); ?></h4>
                        <?php the_content(); ?>
                </div><!--end article-->
            <?php endwhile; //wp_reset_postdata(); ?>

        <?php else : ?>
            <p>no posts</p>
        <?php endif; ?>

        <?php  if ( function_exists( 'foundationpress_pagination' ) ) { foundationpress_pagination(); } else if ( is_paged() ) { ?>
            <nav id="post-nav">
                <div class="post-previous"><?php next_posts_link( __( '&larr; Older posts', 'foundationpress' ) ); ?></div>
                <div class="post-next"><?php previous_posts_link( __( 'Newer posts &rarr;', 'foundationpress' ) ); ?></div>
        <?php } ?>

Here the function of foundationpress_pagination:

function foundationpress_pagination() {
    global $wp_query;

    $big = 999999999; // This needs to be an unlikely integer

    // For more options and info view the docs for paginate_links()
    // http://codex.wordpress.org/Function_Reference/paginate_links
    $paginate_links = paginate_links( array(
        'base' => str_replace( $big, '%#%', html_entity_decode( get_pagenum_link( $big ) ) ),
        'current' => max( 1, get_query_var( 'paged' ) ),
        'total' => $wp_query->max_num_pages,
        'mid_size' => 5,
        'prev_next' => true,
        'prev_text' => __( '&laquo;', 'foundationpress' ),
        'next_text' => __( '&raquo;', 'foundationpress' ),
        'type' => 'list',
    ) );

    $paginate_links = str_replace( "<ul class='page-numbers'>", "<ul class='pagination'>", $paginate_links );
    $paginate_links = str_replace( '<li><span class="page-numbers dots">', "<li><a href='#'>", $paginate_links );
    $paginate_links = str_replace( "<li><span class='page-numbers current'>", "<li class='current'><a href='#'>", $paginate_links );
    $paginate_links = str_replace( '</span>', '</a>', $paginate_links );
    $paginate_links = str_replace( "<li><a href='#'>&hellip;</a></li>", "<li><span class='dots'>&hellip;</span></li>", $paginate_links );
    $paginate_links = preg_replace( '/\s*page-numbers/', '', $paginate_links );

    // Display the pagination if more than one page is found
    if ( $paginate_links ) {
        echo '<div class="pagination-centered">';
        echo $paginate_links;
        echo '</div><!--// end .pagination -->';

In my theme I made a page template called 'page-ar.php' and put in Template Name: Article-page in comments on top. I basically copied this whole query and pasted it on this page. However it just shows the first 5 posts correctly and no pagination is shown. Also typing in '/page/2' sends me to the 404 page.

I really have no clue what the problem is I was hoping someone here did. Also I I set the settings>reading to 5 already so I don't think it's that.

When I display the template page as front page tho the pagination displays correctly tho(with get_query_var('page')). but that's the only way I can get it to work tho.

And as my home I just use index.php

  • Is there a reason why you are using a page + template instead of the post type archive WordPress generates for you? – Milo Mar 16 '16 at 15:48
  • I just wanted all my post with the post_type articles on one page basically, I thought this was the way to go. I don't know about the post type archive but that's a better way to go? – Noob17 Mar 16 '16 at 16:10
  • add a custom page template and place your custom query over there to make it work properly. – Aamer Shahzad Mar 16 '16 at 16:22
  • Hmm weird your answer disappeared. Anyway I pasted '$query['paged'] = get_query_var( 'page' ) ? get_query_var( 'page' ) : 1;' above $query = new Wp_query that didn't seem to work. This code is already in custom page template, so I'll make another one and put the query in there? – Noob17 Mar 16 '16 at 16:26
  • You should not be running custom queries in place of the main query/default loop. They create more issues that what they actually solve, and slows your page down because it does the same job twice. Use pre_get_posts – Pieter Goosen Mar 16 '16 at 18:16

Okay I got it.. Apparently there's going to be some be issues if you name your page name the same as your custom post type. In this case my page was named 'articles' while my custom post type is also named 'articles' so that why I it didn't work. I made a new page tested it and now it works perfectly.

Sorry guys I initially thought it you couldn't have the same template name as your custom page but I confused it was page names. Thank you very much for helping me tho, I really appreciated it!

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.