WordPress內容頁後綴設為html後分頁鏈接為.html/2的解決辦法

注意:解決方法是在justnews主題下設置的,如果是其他主題,請參照着修改

最後效果為

將.html/2替換為_2.html

1、打開wp-content/themes/justnews/themer/functions/pagination.php

搜索

$url = _wp_link_page($i);

在下面添加如下代碼

$url = preg_replace("%(\.html)/(\d*)%", '_' . "$2$1", $url);

2、打開wp-content/themes/justnews/functions.php

最後面添加下面的代碼

class rewrite_inner_page_links
{
  var $separator;
  var $post_rule;
 
  function __construct()
  {
    $this->separator = '—';
    // (.+?)/([^/]+).html(/[0-9]+)?/?
    $this->post_rule = '(.+?)/([^/]+)(' . $this->separator . '([0-9]+))+.html/?$';
    if (!is_admin() || defined('doing_ajax')) :
      add_filter('wp_link_pages_link', array($this, 'inner_page_link_format'), 10, 2); // for inner pages
      add_filter('redirect_canonical', array($this, 'cancel_redirect_for_paged_posts'), 10, 2);
    endif;
    if (is_admin()) :
      add_filter('rewrite_rules_array', array($this, 'pagelink_rewrite_rules'));
    endif;
  }
 
  /**
   * 修改post分頁鏈接的格式
   * @param string $link
   * @param int $number
   * @return string
   */
  function inner_page_link_format($link, $number)
  {
    if ($number > 1)
    {
      if (preg_match('%<a href=".*\.html/\d*"%', $link))
      {
        $link = preg_replace("%(\.html)/(\d*)%", $this->separator . "$2$1", $link);
      }
    }
    return $link;
  }
 
  /**
   * 為新的鏈接格式增加重定向規則,移除原始分頁鏈接的重定向規則,防止重複收錄
   *
   * 訪問原始鏈接將返回404
   * @param array $rules
   * @return array
   */
  function pagelink_rewrite_rules($rules)
  {
    $new_rule[$this->post_rule] = 'index.php?name=$matches[2]&page=$matches[4]';
    return $new_rule + $rules;
  }
 
  /**
   * 禁止wordpress將頁面分頁鏈接跳轉到原來的格式
   * @param string $redirect_url
   * @param string $requested_url
   * @return bool
   */
  function cancel_redirect_for_paged_posts($redirect_url, $requested_url)
  {
    global $wp_query;
    if (is_single() && $wp_query->get('page') > 1)
    {
      return false;
    }
    return true;
  }
}
 
new rewrite_inner_page_links();

3、打開wp-content/themes/justnews/themer/functions/seo.php

搜索

if ( 0 !== $id && $url = wp_get_canonical_url( $id )) {

在下面添加代碼

$url = preg_replace("%(\.html)/(\d*)%", '_' . "$2$1", $url);

4、添加偽靜態規則

rewrite ^/(.*)/([0-9]+)_([0-9]+).html$ /?p=$2&page=$3 last;	

到此為止。

發布者:彬彬筆記,轉載請註明出處:https://www.binbinbiji.com/zh-hant/wordpress/3129.html

(0)
彬彬筆記彬彬筆記
上一篇 2023年4月15日 22:26
下一篇 2023年6月7日 22:09

相關推薦

發表回復

登錄後才能評論
蜀ICP備14017386號-13