admin管理员组文章数量:1326317
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 | Show 1 more comment1 Answer
Reset to default 2So 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.
本文标签: url rewritingPagination custom post type not working with rewrite slug
版权声明:本文标题:url rewriting - Pagination custom post type not working with rewrite slug 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742205670a2432768.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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 atmedias/actualites
currently show? And by "translate", did you mean you're redirectingmedias/actualites
tomedia/press-news
? – Sally CJ Commented Aug 6, 2020 at 14:12medias/actualites
andmedia/press-news
static Pages (i.e. posts of thepage
type)? – Sally CJ Commented Aug 6, 2020 at 15:02has_archive
isfalse
(i.e. archive is not enabled), thenmedias/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