admin管理员组文章数量:1331638
I have custom rewrite rules in place so that the posts page URL can be /blog/
and all posts have the URL structure /blog/post-title-here/
, this works great, however I am getting a 404 error on the /blog/
page pagination when it is structured like /blog/page/2/
-- I've seen many topics and questions on this around StackOverflow, however it appears everything answered is for categories to be structured like /blog/cat-name/page/2/
where I just need it /blog/page/2/
Here is my rewrite function in place to append /blog/
to post URLs:
add_action( 'generate_rewrite_rules', 'site_add_blog_rewrites' );
function site_add_blog_rewrites( $wp_rewrite ) {
$wp_rewrite->rules = array(
'blog/([^/]+)/?$' => 'index.php?name=$matches[1]',
'blog/[^/]+/attachment/([^/]+)/?$' => 'index.php?attachment=$matches[1]',
'blog/[^/]+/attachment/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1',
'blog/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
'blog/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
'blog/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?attachment=$matches[1]&cpage=$matches[2]',
'blog/([^/]+)/trackback/?$' => 'index.php?name=$matches[1]&tb=1',
'blog/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
'blog/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
'blog/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?name=$matches[1]&paged=$matches[2]',
'blog/([^/]+)/?([0-9]{1,})/?$' => 'index.php?name=$matches[1]&paged=$matches[2]',
'blog/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?name=$matches[1]&cpage=$matches[2]',
'blog/([^/]+)/(/[0-9]+)?/?$' => 'index.php?name=$matches[1]&page=$matches[2]',
'blog/[^/]+/([^/]+)/?$' => 'index.php?attachment=$matches[1]',
'blog/[^/]+/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1',
'blog/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
'blog/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
'blog/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?attachment=$matches[1]&cpage=$matches[2]',
) + $wp_rewrite->rules;
}
And then I am converting the post permalinks using:
function site_filter_post_link($permalink, $post) {
if ($post->post_type != 'post') {
return $permalink;
}
return 'blog'.$permalink;
}
add_filter('pre_post_link', 'site_filter_post_link', 10, 2);
Am I missing the proper rewrite to allow the URL structure /blog/page/2/
or is this not possible because WP is thinking /page/
should be a post when it is in this structure? Any help would be greatly appreciated!
NOTE: I cannot have my permalinks structure /%category%/%postname%/
. The permalinks must stay at /%postname%/
I have custom rewrite rules in place so that the posts page URL can be /blog/
and all posts have the URL structure /blog/post-title-here/
, this works great, however I am getting a 404 error on the /blog/
page pagination when it is structured like /blog/page/2/
-- I've seen many topics and questions on this around StackOverflow, however it appears everything answered is for categories to be structured like /blog/cat-name/page/2/
where I just need it /blog/page/2/
Here is my rewrite function in place to append /blog/
to post URLs:
add_action( 'generate_rewrite_rules', 'site_add_blog_rewrites' );
function site_add_blog_rewrites( $wp_rewrite ) {
$wp_rewrite->rules = array(
'blog/([^/]+)/?$' => 'index.php?name=$matches[1]',
'blog/[^/]+/attachment/([^/]+)/?$' => 'index.php?attachment=$matches[1]',
'blog/[^/]+/attachment/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1',
'blog/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
'blog/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
'blog/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?attachment=$matches[1]&cpage=$matches[2]',
'blog/([^/]+)/trackback/?$' => 'index.php?name=$matches[1]&tb=1',
'blog/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
'blog/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
'blog/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?name=$matches[1]&paged=$matches[2]',
'blog/([^/]+)/?([0-9]{1,})/?$' => 'index.php?name=$matches[1]&paged=$matches[2]',
'blog/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?name=$matches[1]&cpage=$matches[2]',
'blog/([^/]+)/(/[0-9]+)?/?$' => 'index.php?name=$matches[1]&page=$matches[2]',
'blog/[^/]+/([^/]+)/?$' => 'index.php?attachment=$matches[1]',
'blog/[^/]+/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1',
'blog/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
'blog/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
'blog/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?attachment=$matches[1]&cpage=$matches[2]',
) + $wp_rewrite->rules;
}
And then I am converting the post permalinks using:
function site_filter_post_link($permalink, $post) {
if ($post->post_type != 'post') {
return $permalink;
}
return 'blog'.$permalink;
}
add_filter('pre_post_link', 'site_filter_post_link', 10, 2);
Am I missing the proper rewrite to allow the URL structure /blog/page/2/
or is this not possible because WP is thinking /page/
should be a post when it is in this structure? Any help would be greatly appreciated!
NOTE: I cannot have my permalinks structure /%category%/%postname%/
. The permalinks must stay at /%postname%/
1 Answer
Reset to default 0If anyone still has this issue (this post is quite old), I found a very helpful snippet in this blog post: https://www.grzegorowski/wordpress-rewrite-rules
/**
* Reprioritise pagination over displaying custom post type content
*/
add_action('init', function() {
add_rewrite_rule('(.?.+?)/page/?([0-9]{1,})/?$', 'index.php?pagename=$matches[1]&paged=$matches[2]', 'top');
});
This will ensure WP uses the pagination rule prior to the post type query rules
本文标签: url rewritingCustom rewrite causes 404 on pagination
版权声明:本文标题:url rewriting - Custom rewrite causes 404 on pagination 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742264872a2443199.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
/blog/%postname%/
? – Milo Commented Jul 30, 2018 at 14:40/blog/
in the URL. That structure is only needed for the 'post' post type, nothing else. Do you know of an easy route to remove that from all post types exceptpost
? Or is it possible to add/blog/
as the slug to thepost
post type registration through a hook or action? – Ty Bailey Commented Jul 30, 2018 at 14:53with_front
set tofalse
so they don't append thepost
front value. If you don't register those post types with your own code, then use theregister_post_type_args
filter like in this answer. – Milo Commented Jul 30, 2018 at 15:38food
I would need that post URL to be/food/food-post-name
. I should also note I have a rewrite in place to remove a custom taxonomy base slug from the URL too. – Ty Bailey Commented Jul 30, 2018 at 16:49