0

I have a custom post type called 'emp_case-studies' and it has a page archive. Here is the code for archive-emp_case-studies.php:

<?php
   $blog = $wp_query;
   $wp_query= null;
   $args= null;

   $args = array(
       'post_type' => 'emp_case-studies'
    );

   $postsperpage = 5;
   $offset = $paged == 0 ? 0 : ($paged-1)*$postsperpage;


   $paging =   array(
      'posts_per_page' => $postsperpage,
      'offset' => $offset
   );

   $ordering =  array(
      'orderby' => 'ID',
      'order' => 'DESC'
   );


   $args = array_merge($args, $ordering, $paging);  
   $wp_query = new WP_Query($args);

?>

<?php
   get_header(); 
   get_template_part( 'include/content', 'head' );
?>

<div class="postcontent nobottommargin<?php if( semi_option( 'blog_sidebar' ) == 'left' ) { echo ' col_last'; } ?> clearfix">
<?php
        if ( $wp_query->have_posts() ) :
        ?>
        <div id="posts" class="small-posts clearfix">
        <?php
            while ( $wp_query->have_posts() ) : $wp_query->the_post();
            ?>

                            <div id="post-<?php $wp_query->ID; ?>" <?php post_class('entry clearfix'); ?>>
                                <?php
                                get_template_part( 'include/blog/news/post', 'standard' );
                                ?>
                            </div>
            <?php                
            endwhile;
            get_template_part( 'include/blog/navigation' );
            ?></div><?php
        else :
            get_template_part( 'include/blog/error' );
        endif;
        $wp_query = $temp;
        ?>
</div>
<?php
get_sidebar('casestudies');
get_template_part( 'include/content', 'foot' ); 
get_footer();
?>

I had a terrible time getting pagination to work correctly so that I could have: archive page: /case-studies single page: /case-studies/post-title

Using the custom query got me the majority of the way. Page 1 and 2 worked fine, but page 3 onward did not. I found a post on the official WordPress forum with someone having the same issue. It was solved by changing Settings > Reading > Blog pages show at most to 1. This worked for me too. All the pagination on my custom post type archive pages work perfectly.

However, it means that now my blog and search results page are only showing one post per page.

Is there any way I can change this to 5 posts per page?

P.S. My theme includes this function to provide pagination. It is used on both blogs and the search results page.

function semi_pagination( $pages = '', $range = 4 ) {

     $showitems = ($range * 2) + 1;  

     global $paged;

     if( empty( $paged ) ) $paged = 1;

     if( $pages == '' ) {

         global $wp_query;

         $pages = $wp_query->max_num_pages;

         if(!$pages) {

             $pages = 1;

         }

     }   

     if(1 != $pages) {

         echo "<div class=\"pagination pagination-centered clearfix nobottommargin topmargin\" style=\"font-size: 13px;\"><ul>";

         if( $paged > 2 && $paged > $range + 1 && $showitems < $pages ) echo "<li><a href='" . get_pagenum_link( 1 ) . "'>&lArr;</a></li>";

         if( $paged > 1 && $showitems < $pages ) echo "<li><a href='" . get_pagenum_link( $paged - 1 ) . "'>&laquo;</a></li>";

         for ( $i = 1; $i <= $pages; $i++ ) {

             if ( 1 != $pages && ( !( $i >= $paged + $range + 1 || $i <= $paged - $range - 1 ) || $pages <= $showitems )) {

                 echo ( $paged == $i ) ? "<li><span class=\"active\">" . $i . "</span></li>" : "<li><a href='" . get_pagenum_link( $i ) . "'>" . $i . "</a></li>";

             }

         }

         if ( $paged < $pages && $showitems < $pages ) echo "<li><a href=\"" . get_pagenum_link( $paged + 1 ) . "\">&raquo;</a></li>";

         if ( $paged < $pages-1 &&  $paged + $range - 1 < $pages && $showitems < $pages ) echo "<li><a href='" . get_pagenum_link( $pages ) . "'>&rArr;</a></li>";

         echo "</ul></div>\n";

     }

}
  • please search for the pre_get_posts action on this site and in codex. this question has been asked and answered many times here. don't try to modify main queries in the template, your "solution" to the first problem only masked the problem, it did not solve it. – Milo Feb 17 '14 at 15:20
  • Thank you, Milo. You're a life-saver! I'll write up the answer with the corrected code for anyone else with the same issue. – Chris Feb 17 '14 at 16:22
0

Thank you to Milo for pointing me to the pre_get_posts hook. That was exactly what I needed.

Here is my updated code:

Archive page code

<?php
   get_header(); 
   get_template_part( 'include/content', 'head' );
?>

<div class="postcontent nobottommargin<?php if( semi_option( 'blog_sidebar' ) == 'left' ) { echo ' col_last'; } ?> clearfix">
<?php
        if ( have_posts() ) :
        ?>
        <div id="posts" class="small-posts clearfix">
        <?php
            while ( have_posts() ) : the_post();
            ?>

                            <div id="post-<?php the_ID(); ?>" <?php post_class('entry clearfix'); ?>>
                                <?php
                                get_template_part( 'include/blog/casestudies/post', 'standard' );
                                ?>
                            </div>
            <?php                
            endwhile;
            get_template_part( 'include/blog/navigation' );
            ?></div><?php
        else :
            get_template_part( 'include/blog/error' );
        endif;
        ?>
</div>
<?php
get_sidebar('casestudies');
get_template_part( 'include/content', 'foot' ); 
get_footer();
?>

I then added this to functions.php from the codex

function hwl_home_pagesize( $query ) {


    if ( is_post_type_archive( 'emp_case-studies' ) ) {
        $query->set( 'posts_per_page', 5 );
        return;
    }
}
add_action( 'pre_get_posts', 'hwl_home_pagesize', 1 );

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.