-1

I am trying to display pagination from archive.php with my custom post type news but when I click to Next page I am getting 404 page

My Custom Post Type Code:

function codex_custom_init() {
  $labels = array(
    'name'               => 'News',
    'singular_name'      => 'News',
    'add_new'            => 'Add News',
    'add_new_item'       => 'Add New News',
    'edit_item'          => 'Edit News',
    'new_item'           => 'New News',
    'all_items'          => 'All News',
    'view_item'          => 'View News',
    'search_items'       => 'Search News',
    'not_found'          => 'No news found',
    'not_found_in_trash' => 'No news found in Trash',
    'parent_item_colon'  => '',
    'menu_name'          => '1. News'
  );

  $args = array(
    'labels'             => $labels,
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'rewrite'            => array( 'slug' => 'news' ),
    'capability_type'    => 'post',
    'has_archive'        => true,
    'hierarchical'       => false,
    'menu_position'      => null,
    'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
  );

  register_post_type( 'news', $args );
}
add_action( 'init', 'codex_custom_init' );

and My Pagination Code:

<?php 
    // the query to set the posts per page to 3
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array('posts_per_page' => 2, 'post_type' => 'news' , 'paged' => $paged );
    query_posts($args); 
?>
<!-- the loop -->
<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>
    <div class="news-main">
        <div class="news-image"><img src="<?php echo get_field('image'); ?>" /></div>
        <div class="news-container">
            <h1><?php echo the_title(); ?></h1>
            <p><?php echo the_content(); ?></p>
        </div>
    </div>
<?php endwhile; ?>
<!-- pagination -->
    <?php next_posts_link(); ?>
    <?php previous_posts_link(); ?>
<?php else : ?>
<!-- No posts found -->
<?php endif; ?>

I am new with WordPress so please some one help me :).

0

1 Answer 1

1

Your code works fine with me, maybe you need to flush your rewrite rules by just visiting the Settings->Permalinks screen on your dashboard.

Note: Visiting the Permalinks screen triggers a flush of rewrite rules. There is no need to save just to flush the rewrite rules.

http://codex.wordpress.org/Settings_Permalinks_Screen#Customize_Permalink_Structure

Not the answer you're looking for? Browse other questions tagged or ask your own question.