最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

permalinks - Pagination of custom post type leads to 404 error

programmeradmin1浏览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/?page_id=111&paged=2

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

mysite/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

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/?page_id=111&paged=2

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

mysite/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

Share Improve this question asked Jul 22, 2015 at 9:06 BenmayBenmay 3617 silver badges17 bronze badges 1
  • Look into this answer, I hope may be this can help you: wordpress.stackexchange/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
Add a comment  | 

3 Answers 3

Reset to default 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.

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' );

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>
发布评论

评论列表(0)

  1. 暂无评论