How to paginate custom post type in custom page

Hi everyone I’m trying to make pagination in custom template page for custom post type. When I click Older entries it redirects me to localhost/videos/page/2/ which is ok but it displays index.php content not videos.php. Here is my videos.php:

<?php
/*Template name: Videos*/
get_header(); ?>

<div class="wrapper">

    <div class="row home-row slider-box" id="sliders">

            <?php
               $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                // The Query
                $the_query = new WP_Query( array(
                    'post_status'=>'publish',
                     'post_type' => 'videos',
                      'orderby' => 'date',
                       'order' => 'DESC',
                        'posts_per_page' => '1',
                         'paged'=>$paged ) );
                $count = 1;
                // The Loop
                while ( $the_query->have_posts() ) : $the_query->the_post();
                     get_template_part('content', 'videos');
                endwhile;


            next_posts_link( 'Older Entries', $the_query->max_num_pages );
    previous_posts_link( 'Newer Entries' );
?>

</div>

And here is how my functions looks:

Read More
function videos(){
    $args = array(
        'public' => true,
        'label' => __('Videos'),
        'rewrite' => array('slug' => 'videos', 'with_front' => true),
        'supports' => array('title', 'editor', 'thumbnail'));
    register_post_type('videos', $args);
    register_taxonomy('videos', 'nagrody');
}
add_action('init', 'videos');

Why this pagination doesn’t work properly?

Related posts

Leave a Reply

1 comment

  1. WordPress pagination works on the main query. So on this page the pagination would work on the page not the WP_Query you have added since the main query will be to get the videos page.

    If you set has_archive to true when creating your videos post type you can create an archive page for it under archive-videos.php and use that to display the posts, and pagination will work.

    $args = array(
            'public' => true,
            'has_archive' => true,
            'label' => __('Videos'),
            'rewrite' => array('slug' => 'videos', 'with_front' => true),
            'supports' => array('title', 'editor', 'thumbnail'));
    

    You may be able to fix it without the archive page, try reading here https://wordpress.stackexchange.com/questions/120407/how-to-fix-pagination-for-custom-loops