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

url rewriting - Pagination custom post type not working with rewrite slug

programmeradmin5浏览0评论

I can't find the way of making my pagination to work on my post type (this is not on an archive-posttype.php). I've rewrited my post type, but the second page isn't working if I do the rewriting ("medias/actualites"), but it works if I have the non-rewrited slug ("actualite").

I do the rewrite because my client wants to have the "medias/actualites" in the url, but with that, the pagination return a 404 on the second page...

This is my post type

$args = array(
    'menu_icon' => 'dashicons-admin-site-alt2',
    'label' => __('Actualités', 'domain'),
    'description' => __('Actualités', 'domain'),
    'labels' => $labels,
    'supports' => array('title', 'page-attributes', 'editor', 'thumbnail', 'revisions', 'post-formats', 'excerpt',),
    'taxonomies' => array('category', 'post_tag'),
    'hierarchical' => true,
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_position' => 5,
    'show_in_admin_bar' => true,
    'show_in_nav_menus' => true,
    'can_export' => true,
    'has_archive' => false,
    'exclude_from_search' => false,
    'publicly_queryable' => true,
    'capability_type' => 'page',
    'rewrite' => array(
        'slug' => 'medias/actualites',
        'with_front' => false,
    ),

);

Edit : and I'm using a plugin to translate /medias/actualites to /media/press-news

I can't find the way of making my pagination to work on my post type (this is not on an archive-posttype.php). I've rewrited my post type, but the second page isn't working if I do the rewriting ("medias/actualites"), but it works if I have the non-rewrited slug ("actualite").

I do the rewrite because my client wants to have the "medias/actualites" in the url, but with that, the pagination return a 404 on the second page...

This is my post type

$args = array(
    'menu_icon' => 'dashicons-admin-site-alt2',
    'label' => __('Actualités', 'domain'),
    'description' => __('Actualités', 'domain'),
    'labels' => $labels,
    'supports' => array('title', 'page-attributes', 'editor', 'thumbnail', 'revisions', 'post-formats', 'excerpt',),
    'taxonomies' => array('category', 'post_tag'),
    'hierarchical' => true,
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_position' => 5,
    'show_in_admin_bar' => true,
    'show_in_nav_menus' => true,
    'can_export' => true,
    'has_archive' => false,
    'exclude_from_search' => false,
    'publicly_queryable' => true,
    'capability_type' => 'page',
    'rewrite' => array(
        'slug' => 'medias/actualites',
        'with_front' => false,
    ),

);

Edit : and I'm using a plugin to translate /medias/actualites to /media/press-news

Share Improve this question edited Aug 6, 2020 at 13:46 Morgan asked Aug 6, 2020 at 13:36 MorganMorgan 13710 bronze badges 6
  • Are you referring to medias/actualites/page/2 and pages 3, 4, etc.? If so, then you should enable archive for the post type, i.e. 'has_archive' => true. But what does the page at medias/actualites currently show? And by "translate", did you mean you're redirecting medias/actualites to media/press-news? – Sally CJ Commented Aug 6, 2020 at 14:12
  • Yes, I'm refering to those pages. I wanted to do this by archive-actualite, but my plugin (polylang (not pro)) doesn't accept the translation of the page title. medias/actualites shows all of my actualite with a custom wp_query – Morgan Commented Aug 6, 2020 at 14:36
  • Lol sorry about the "translate" thing.. So are the medias/actualites and media/press-news static Pages (i.e. posts of the page type)? – Sally CJ Commented Aug 6, 2020 at 15:02
  • When has_archive is false (i.e. archive is not enabled), then medias/actualites will naturally result in a 404 error, but you said that it displays all the posts in your CPT, hence I asked if you've got a static page at that URL. But even so, the 404 error on pages 2, 3, etc. is normal since WordPress treats the URL (e.g. medias/actualites/page/2) as a single CPT request. – Sally CJ Commented Aug 6, 2020 at 17:26
  • It display my posts because it's indeed a custom page (as I said, not archive-actualite.php). So how can I "fix" the 404 ? – Morgan Commented Aug 7, 2020 at 7:52
 |  Show 1 more comment

1 Answer 1

Reset to default 2

So based on the comments, you confirmed that you're using a custom Page (i.e. post of the page type) as the post type's archive, which means you either have both the media and actualites Pages (where actualites is a child of the media Page) or that you used a parent Page for the CPT archive.

And normally, going to page 2, 3, etc. of a Page will not going to result in a 404 error; however, your post type's rewrite slug is medias/actualites, therefore medias/actualites/page/2 (or page/3, page/4, etc.) will result in the 404 error because WordPress treats the URL as a single CPT request.

But fortunately, you can get rid of the 404 error by using a custom rewrite rule which can be added using add_rewrite_rule():

// First, register the CPT.
register_post_type( 'actualite', array( ... your args ... ) );

// Then add the rewrite rule.
// Note: With pagename, you must use the full page path, i.e. <parent slug>/<child slug>
add_rewrite_rule(
    '^medias/actualites/page/(\d+)/?$',
    'index.php?pagename=medias/actualites&paged=$matches[1]',
    'top'
);
/* Or if using a parent Page:
add_rewrite_rule(
    '^medias/actualites/page/(\d+)/?$',
    'index.php?pagename=your-cpt-archive&paged=$matches[1]',
    'top'
);
*/

PS: Don't forget to flush the rewrite rules and just use get_query_var( 'paged' ) to get the page number.

And if you already have a custom rewrite rule, you can add it to the question and I'll help you fix it.

发布评论

评论列表(0)

  1. 暂无评论