error 404 at home.php when pagination with post name in permalink

Question

I’m building a blog for my website so I did the home.php. To take the posts, I do the following code:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$posts = new WP_Query(array(
  'post_type'      => 'post',
  'posts_per_page' => 1,
  'paged'          => $paged,
));
$categories = get_categories();
?>

after loop and show the posts inside the $posts, I call a function to invoke my pagination:

<?php if ($posts->max_num_pages > 1) : ?>
<div class="pagination mt-10">
  <?= yourtheme_paging_nav($posts);  ?>
</div>
<?php endif;  ?>

the yourtheme_paging_nav() function has the following code:

function yourtheme_paging_nav($queried_posts = null) {
    if ($queried_posts == null) {
        global $wp_query;
        $queried_posts = $wp_query;
    }

    if( is_singular() )
        return;
    
    /** Stop execution if there's only 1 page */
    if( $queried_posts->max_num_pages <= 1 )
        return;
    
    $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
    $max   = intval( $queried_posts->max_num_pages );
    
    /** Add current page to the array */
    if ( $paged >= 1 )
        $links[] = $paged;
    
    /** Add the pages around the current page to the array */
    if ( $paged >= 3 ) {
        $links[] = $paged - 1;
        $links[] = $paged - 2;
    }
    
    if ( ( $paged + 2 ) <= $max ) {
        $links[] = $paged + 2;
        $links[] = $paged + 1;
    }
    
    echo '<div class="pagination mt-10">' . "n";
    
    /** Previous Post Link */
    if ( get_previous_posts_link() )
        printf( '<a href="%s"><span class"prev-class"></span></a>' . "n", get_previous_posts_page_link() );
    
    /** Link to first page, plus ellipses if necessary */
    if ( ! in_array( 1, $links ) ) {
        $class = 1 == $paged ? 'class="active"' : '';
        printf( '<a %s href="%s">%s</a>' . "n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
        if ( ! in_array( 2, $links ) )
            echo '<a>…</a>';
    }
    
    /** Link to current page, plus 2 pages in either direction if necessary */
    sort( $links );
    foreach ( (array) $links as $link ) {
        $class = $paged == $link ? ' class="active"' : '';
        printf( '<a %s href="%s">%s</a>' . "n", $class, esc_url( get_pagenum_link( $link ) ), $link );
    }
    
    /** Link to last page, plus ellipses if necessary */
    if ( ! in_array( $max, $links ) ) {
        if ( ! in_array( $max - 1, $links ) )
            echo '<a>…</a>' . "n";
    
        $class = $paged == $max ? ' class="active"' : '';
        printf( '<a %s href="%s">%s</a>' . "n", $class, esc_url( get_pagenum_link( $max ) ), $max );
    }
    
    /** Next Post Link */
    if ( get_next_posts_link() )
        printf( '<a href="%s"><span class="next-class"></span></a>' . "n", get_next_posts_page_link() );
    
    echo '</div>' . "n";
    }

The pagination in the frontend is working well and the page’s links are something like "/page/2" but, when I leave the first page and go to another, I took the error 404. The permalink structure is seated to "post name".

The URL of my blog in my localhost project: http://josefernandesvilas.local.com/blog/
The second page is: http://josefernandesvilas.local.com/blog/page/2/

0
Mikhael Araujo 2 months 0 Answers 17 views 0

Leave an answer