自定义重写导致404分页

时间:2018-07-30 作者:Ty Bailey

我有自定义的重写规则,以便帖子页面URL可以/blog/ 所有帖子都有URL结构/blog/post-title-here/, 这很好,但是我在/blog/ 页面分页的结构如下/blog/page/2/ -- 我看到了many 有关StackOverflow的主题和问题,但似乎所有回答都是针对类别的/blog/cat-name/page/2/ 在我需要的地方/blog/page/2/

下面是我要附加的重写函数/blog/ 发布URL:

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;
}
然后我使用以下方法转换后permalinks:

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);
我是否缺少允许URL结构的正确重写/blog/page/2/ 或者这不可能,因为WP正在思考/page/ 在这个结构中应该是一个帖子吗?任何帮助都将不胜感激!

NOTE:cannot 有我的permalinks结构/%category%/%postname%/. permalinks必须留在/%postname%/

1 个回复
SO网友:Red Roboto

如果有人仍然有这个问题(这篇文章很旧),我在这篇博文中找到了一个非常有用的片段:https://www.grzegorowski.com/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\');
});
这将确保WP在post类型查询规则之前使用分页规则

结束

相关推荐

Taxonomy Pagination Rewrite

我有一个名为“推荐信”的自定义帖子类型,其中包含reviews. 我还有一个名为“推荐部门”的分类法reviews. 目标是我将拥有像/reviews/sales/ 或/reviews/service/. 除了分页之外,一切都按照我的意愿进行。我尝试了所有不同的提示和技巧,以使分页按预期工作,但不管怎样/reviews/sales/page/2 就是不管用。但是,如果我手动输入/reviews/sales/?page=2 正如人们所期望的那样,这将导致第二页的结果。是否有允许我使用查询字符串的重写规则?p