Home » Ask (Forum) » Problem with Custom Post Type in Index.php


Good day, I have created custom post type “book”. I want to show that custom post type in my index.php file. Yes, everything work fine, until I go to second page, I got 404 error.

I’m using this code:

	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;	
	$wp_query = new WP_Query( array(
		'post_type' => array( 'book' ),
		'posts_per_page' => 5,
		'paged' => $paged,
<?php if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>

<?php echo get_the_title(); ?>

<?php endwhile; ?>	
<div class="pagination">
	<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
	<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php wp_reset_query(); wp_reset_postdata(); ?>
<?php else : ?>
<?php endif; ?>

Can someone help me to solve my problem, please!

Sep 16, 2016 (3 years ago) - Posted by 1 Comment

One Answer

  1. Support says:

    Hi, may I know why you put custom post type in index.php file? Why not use archive file?

    Replace this line:

    <?php next_posts_link( 'Older posts' ); ?>

    With this code:

    <?php next_posts_link('Older Entries', $wp_query->max_num_pages); ?>

    I never try this code before, but i hope everything work fine.

    Good luck!

    Team Support

Your Answer

Your email address will not be published. Required fields are marked *