0

This seems to be common issue, and there exist a lot of posts about this issue but none of them works for me.

I have a custom post type (portfolio) with a page pagination.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
echo '<a href="'.get_pagenum_link($paged+1).'">Next Page</a>';

When Set the permalinks settings to 'Default' it works fine.

mysite.com/?page_id=111&paged=2

When Set the permalinks to any custom structure it leads to the 404 page.

mysite.com/portfolio/page/2/

This is driving me crazy. Can't find any solution. I don't have a page or post which have the slugname 'page', so this can't be the problem. I refreshed the permlinks multiple times, which doesn't work either.

NB: default Blog page works fine for both permalinks settings

I would really appreciate some hlep on this. Thanks

1
  • Look into this answer, I hope may be this can help you: wordpress.stackexchange.com/questions/264284/… if you not able to solve the problem, then please share your code you are using to fetch posts from WP. Because without that I don't think the question can be answered properly.
    – BlueSuiter
    Commented Apr 15, 2019 at 13:34

3 Answers 3

1

Paste this function to your functions.php file

if ( ! function_exists( 'custom_pagination' ) ) {
    function custom_pagination( $query_args ) {
        $big = 999999999; // need an unlikely integer
        $pages = paginate_links( array(
                'base'      => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                'format'    => '?paged=%#%',
                'current'   => max( 1, get_query_var( 'paged' ) ),
                'total'     => $query_args->max_num_pages,
                'prev_next' => false,
                'type'      => 'array',
                'prev_next' => true,
                'prev_text' => __('«'),
                'next_text' => __('»'),
            ) );
        if ( is_array( $pages ) ) {
            $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
            echo '<ul class="pagination">';
            foreach ( $pages as $page ) {
                    echo "<li>$page</li>";
            }
            echo '</ul>';
        }
    }
}

Then on your custom post loop page write the loop as

$args = [
    'post_type'      => 'post',
    'posts_per_page' => get_option('posts_per_page'),
];

$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // You content goes here
    }
    custom_pagination( $query );
    wp_reset_postdata();
}

This works fine for me.

1
  • What's the purpose of this line $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');? Didn't find any use of it!
    – obiPlabon
    Commented Feb 19, 2018 at 11:15
0

I had the same case and I was going crazy and tested Disable canonical redirection for this case and worked. Maybe this will also help

I'm not sure if this would be the best way to do this , however.

function no_canonical( $url ) {
        return false;
}
function adjust_show_request( $request ) {
        if ($request->query_vars['post_type'] === 'agent' && $request->is_singular === true && $request->current_post == -1 && $request->is_paged === true ) {
                add_filter( 'redirect_canonical', 'no_canonical' );
        }
        return $request;
}
add_action( 'parse_query', 'adjust_show_request' );
0

try with this for page pagination. your page next previous order depends on menu order. check get_pages arg. code put into your theme page template.

<?php
$pagelist = get_pages('sort_column=menu_order&sort_order=asc');
$pages = array();
foreach ($pagelist as $page) {
   $pages[] += $page->ID;
}

$current = array_search(get_the_ID(), $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>

<div class="navigation">
<?php if (!empty($prevID)) { ?>
<div class="alignleft">
<a href="<?php echo get_permalink($prevID); ?>"
  title="<?php echo get_the_title($prevID); ?>">Previous</a>
</div>
<?php }
if (!empty($nextID)) { ?>
<div class="alignright">
<a href="<?php echo get_permalink($nextID); ?>" 
 title="<?php echo get_the_title($nextID); ?>">Next</a>
</div>
<?php } ?>
</div>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.