0

After having problems getting pagination working at all for custom post types, I've read that the custom post type name and the page name cannot be the same. So I set out from scratch:

I've created a custom post type of 'news-articles' (original I called it 'news' but got 404s on pagination) using the Custom Post Type UI plugin.

Next I created a template called template-news.php with the following content:

<?php
/**
 * Template Name: News
 *
 * Print posts of a Custom Post Type.
 */

get_header(); 
get_template_part( 'include/content', 'head' );

$type = 'news-articles';

if ( get_query_var('paged') ) {
   $paged = get_query_var('paged');
} else if ( get_query_var('page') ) {
   $paged = get_query_var('page');
} else {
   $paged = 1;
}

$args=array(
   'post_type' => $type,
   'post_status' => 'publish',
   'paged' => $paged,
   'posts_per_page' => 1,
   'caller_get_posts'=> 1
);

$temp = $wp_query;  // assign original query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query($args);
?>

<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 if( semi_option( 'blog_sidebar' ) == 'left' ) { get_sidebar( 'left' ); } else { echo get_sidebar('news'); } ?>
<?php get_template_part( 'include/content', 'foot' ); ?>   
<?php get_footer(); ?>

Next I created a page called 'News' and assigned the template as 'News'.

At this point, if I view the news page, everything looks good. I get pagination and it works. However post title links are /news-article/post-title instead of /news/post-title.

If I try going to /news/post-title it just redirects to /news-article/post-title.

I tried setting 'Rewrite' to 'True' and 'Custom Rewrite Slug' to 'news' in CPT UI. At first glance this appears to have cured it, but it breaks pagination - I get a 404 on page 2. I've tried refreshing the permalinks but still get the 404.

  • Why can't you use archive-{post_type}.php and single-{post_type}.php for this? See: codex.wordpress.org/Template_Hierarchy – s_ha_dum Jan 31 '14 at 15:08
  • Thanks for the reply. Yeah I tried deleting the 'news' page, creating a custom post type of 'news' with archive set to true. Created archive-news.php. Pagination results in 404 :( – Chris Jan 31 '14 at 15:32
  • If you use the archive- and single- files you should not have a custom query in them. – s_ha_dum Jan 31 '14 at 15:34
  • I'm at a loss as to where to start now :( – Chris Jan 31 '14 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.