0

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

3
  • 1
    Why are you using new WP_Query()? Look at the default themes, that's not how you do category.php. Commented Mar 24, 2022 at 21:24
  • if you want to change what posts are shown in the main query, you should use the pre_get_posts filter with a normal post loop, not a second custom query to replace it. This is why you are having pagination issues, this is also slowing down your site
    – Tom J Nowell
    Commented Mar 24, 2022 at 23:09
  • I thought that I have to use WP_query since it's an custom taxonomy. So far I know it understands "paged" so the pagination should work..... Maybe you can throw in an good link with an example because so far the ones I have been Googleing doesn't give me the results I want. The page should show specific posts from specific categories not all of them.
    – in2d
    Commented Mar 25, 2022 at 3:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.