0

For a project I have created a custom post type called "episodes", which is registered like this.

 register_post_type( 'episodes',
array(
  'labels' => array(
    'name' => __( 'Episodes' ),
    'singular_name' => __( 'Episode' )
  ),
  'public' => true,
  'has_archive' => false,
  'hierarchical' => true,
  'rewrite' => array( 'slug' => 'episodes'),
)
);

I have also created a page called episodes. The idea is that I want a page with some basic information and underneath a grid with 8 episodes which can be clicked through using pagination. To be clear, the content of the episodes page should alway show, even on page 2 or 3 of the grid.

To achieve this I created a page-episodes.php file, copied the content of page.php and added the following code to the page:

<?php 
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

        $args = array(
            'post_type' => 'episodes',
            'posts_per_page' => 8,
            'paged'          => $paged,
        );

        $query = new WP_Query( $args );
    ?>

    <div class="row">
<?php if($query->have_posts()) : while($query->have_posts()) : $query->the_post(); ?>
    <div class="col-3 episode">
      <a href="<?php the_permalink()?>">
        <span class="episodeNumber"><?php echo get_field("episode_number", $post->ID)?></span>
        <h1><?php the_title() ?></h1>
      </a>
    </div>
  <?php endwhile; endif; ?>

</div>
<div class="row">
  <div class="col-12 pagination">
            <?php 
                echo paginate_links();
            ?>
  </div>
</div>

But whenever I use the pagination and go to /episodes/page/2 for example the page returns the 404.php page.

I hope I have given you enough information, if not please ask. I would really like to know what I'm doing wrong here.

0

WordPress does this for you. You shouldn't need a custom Page template or custom query and pagination.

Just set 'has_archive' to true when registering the post type. Then you automatically get /episodes and /episodes/page/X.

Then you just need to create archive-afleveringen.php for the template, and in that template use the functions for the main query:

<?php if( have_posts()) : while( have_posts()) : the_post(); ?>

You also don't need to create your own query or used paged at all.

  • I would like to have the content above the grid/list with episodes editable in the backend, is this possible if I use the archive page? – M. Dillen May 25 '18 at 12:46

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.