WordPress单篇文章分页静态化错误的处理方法

三、/%year%/%monthnum%%day%/%post_id%.html

// 添加分页处理规则
function add_custom_post_rewrite_rules($rules) {
  $custom_rules = array(
    '([0-9]{4})/([0-9]{1,2})([0-9]{1,2})/([0-9]+)-([0-9]+)\.html$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&p=$matches[4]&page=$matches[5]',
  );
  $rules = array_merge($custom_rules, $rules);

  return $rules;
}
add_filter('post_rewrite_rules', 'add_custom_post_rewrite_rules');

// 修改分页链接
function my_wp_link_pages($args = '') {
  $args .= ($args ? '&' : '') . 'echo=0';
  $links = wp_link_pages($args);
  $links = preg_replace_callback('|([0-9]{4}/[0-9]{1,2}[0-9]{1,2}/)([0-9]+)(\.html)(/)([0-9]+)|', 'custom_page_link', $links);

  echo $links;
}

function custom_page_link($matches) {
  return $matches[1].$matches[2].'-'.$matches[5].$matches[3];
}
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^([0-9]{4})/([0-9]{1,2})([0-9]{1,2})/([0-9]+)\.html/trackback/?$ index.php?year=$1&monthnum=$2&day=$3&p=$4&tb=1 [L]
RewriteRule ^([0-9]{4})/([0-9]{1,2})([0-9]{1,2})/([0-9]+)\.html/feed/(feed|rdf|rss|rss2|atom)/?$ index.php?year=$1&monthnum=$2&day=$3&name=$4&feed=$5 [L]
RewriteRule ^([0-9]{4})/([0-9]{1,2})([0-9]{1,2})/([0-9]+)\.html/(feed|rdf|rss|rss2|atom)/?$ index.php?year=$1&monthnum=$2&day=$3&p=$4&feed=$5 [L]
RewriteRule ^([0-9]{4})/([0-9]{1,2})([0-9]{1,2})/([0-9]+)\.html/page/?([0-9]{1,})/?$ index.php?year=$1&monthnum=$2&day=$3&p=$4&paged=$5
RewriteRule ^([0-9]{4})/([0-9]{1,2})([0-9]{1,2})/([0-9]+)\.html/([0-9]+)/?$ index.php?year=$1&monthnum=$2&day=$3&p=$4&page=$5
</IfModule>

四、/%category%-%post_id%.html

// 添加分页处理规则
function add_custom_post_rewrite_rules($rules) {
  $custom_rules = array(
    '.+-([0-9]+)-([0-9]+)\.html$' => 'index.php?p=$matches[1]&page=$matches[2]',
  );
  $rules = array_merge($custom_rules, $rules);

  return $rules;
}
add_filter('post_rewrite_rules', 'add_custom_post_rewrite_rules');

// 修改分页链接
function my_wp_link_pages($args = '') {
  $args .= ($args ? '&' : '') . 'echo=0';
  $links = wp_link_pages($args);
  $links = preg_replace_callback('|([^<>]+-)([0-9]+)(\.html)(/)([0-9]+)|', 'custom_page_link', $links);

  echo $links;
}

function custom_page_link($matches) {
  return $matches[1].$matches[2].'-'.$matches[5].$matches[3];
}
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^.+-([0-9]+)\.html/trackback/?$ index.php?p=$1&tb=1 [L]
RewriteRule ^.+-([0-9]+)\.html/feed/(feed|rdf|rss|rss2|atom)/?$ index.php?p=$1&feed=$2 [L]
RewriteRule ^.+-([0-9]+)\.html/(feed|rdf|rss|rss2|atom)/?$ index.php?p=$1&feed=$2 [L]
RewriteRule ^.+-([0-9]+)\.html/page/?([0-9]{1,})/?$ index.php?p=$1&paged=$2
RewriteRule ^.+-([0-9]+)\.html/([0-9]+)/?$ index.php?p=$1&page=$2
</IfModule>


0%(0)

100%(3)
发表评论?

25 条评论。

  1. 文章挺不错的,写得很好

  2. 不错不错,虽然网上有很多类似的,但博主的钻研劲头是难得的

  3. 呵呵,楼主说的很好的

发表评论

*