treehouse : what would you like to learn today?
Web Design Web Development iOS Development

WordPress Pagination & Custom post types

  • I'm having a problem getting this pagination working it always shows 404 error when I click next.

    <ul class="m-grid cf">
    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array('post_type' => 'portfolio','posts_per_page'=> 9, 'order' => 'DESC', 'paged' => $paged );
    query_posts( $args );
    if ( have_posts() ) :
    while ( have_posts() ) : the_post();?>
    <li class="mod-third"><a href="<?php the_permalink();?>">
    <figure class="face thumb">
    <?php
    if ( has_post_thumbnail() ) {
    the_post_thumbnail('portfolio-thumb');
    }
    ?>
    </figure>
    <h3 class="back"><?php the_title();?></h3>
    </a></li>
    <?php endwhile;


    //Pagination
    if(function_exists('my_paginate_links')) {
    my_paginate_links();
    }

    endif;

    wp_reset_postdata();
    wp_reset_query();

    ?>
    </ul>

    & here is the pagination function

    function my_paginate_links() {
    global $wp_rewrite, $wp_query;
    $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
    $pagination = array(
    'base' => @add_query_arg('page','%#%'),
    'format' => '',
    'total' => $wp_query->max_num_pages,
    'current' => $current,
    'prev_text' => __('« Previous page'),
    'next_text' => __('Next page »'),
    'end_size' => 1,
    'mid_size' => 2,
    'show_all' => true,
    'type' => 'list'
    );
    if ( $wp_rewrite->using_permalinks() )
    $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );
    if ( !empty( $wp_query->query_vars['s'] ) )
    $pagination['add_args'] = array( 's' => get_query_var( 's' ) );
    echo paginate_links( $pagination );
    }

    So what's wrong & how can I fix this?

    Thanks.
  • anyone :)?
  • Any chance you can show how this ends up on the site? Where do the pagination links point to?
  • The pagination link is /page/{page number} so when I click next page /page/2/ I get the 404 page.

    Unfortunately I can't show the site.
  • Do any other permalinks work? Like http://www.yoursite.com/hello-world and such?
  • Yeah the whole websites links works perfectly it's just the pagination links