wp query – Why is my pagination /page/2 taking me to index.php?

Question

Why does my pagination take me to sites front page instead of page 2. The url seems to be correct mydomain.com/category/categroyname/page/2 but it doesnt display posts for 2 page. I tried to reset my page permalinks but it still does the same thing. Right now I have category.php in my file structure, I tried to add page.php but it still takes me to front page.

<?php

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

$args = array(
    'post_type' => 'myposttype',
    'posts_per_page' => 1,
    'paged' => $paged,
    'tax_query' => array(
        array (
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => 'myterm',
        )
    ),
);
    
    $my_query = new WP_Query( $args ); ?>

<ul>

    <?php

    if( $my_query->have_posts() ) :
       
        while ($my_query->have_posts()) : $my_query->the_post(); 
        
        ?>
            
            <li>
           
            <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
      
                <?= get_the_post_thumbnail($post_item['ID'], 'full'); ?>
           
           
                <h4><?php the_title(); ?></h4>
                <span class="title-description"><?= wp_trim_words( get_the_content(), 25, '..'); ?></span>

    </a>
        </li>
      
            <?php
        endwhile; endif;

    wp_reset_query();
?>

<?php
$total_pages = $my_query->max_num_pages;

if ($total_pages > 1){

    $current_page = max(1, get_query_var('paged'));

    echo paginate_links(array(
        'base' => get_pagenum_link(1) . '%_%',
        'format' => '/page/%#%',
        'current' => $current_page,
        'total' => $total_pages,
        'prev_text'    => __('« prev'),
        'next_text'    => __('next »'),
    ));
}

?>
<?php wp_reset_postdata();?>

When I change page number in WP_query array for example ‘paged’ => 2, or ‘paged’ => 3, then the posts change on the page. For some reason the site doesn’t understand what to show on /page/2.

This is my file structure. Am I missing something?

enter image description here

0
in2d 5 months 2022-03-24T14:17:00-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse