使用WordPress永久鏈接的終極指南

永久鏈接是用於訪問您網站上特定內容的鏈接。

#js-mykinsta-video {
背景圖片:url(https://kinsta.com/wp-content/themes/kinsta/images/mykinsta-dashboard-v8@2x.jpg);
}

免費試用

例如,金斯塔(Kinsta)的主頁位於https://kinsta.com/,我們的博客位於https://kinsta.com/blog/,單個帖子使用的鏈接如https://kinsta.com/blog/ wordpress-widgets /。

永久鏈接還用於存檔頁面,靜態頁面以及您網站上需要其自身URL的任何內容。

在這篇文章中,我們將向您展示永久鏈接的工作方式,如何針對您的網站進行優化,以及如何通過設置屏幕和編寫一些代碼進行配置。

什麼是WordPress永久鏈接?

WordPress永久鏈接在官方WordPress Codex上定義為:

「指向您個人博客文章的永久URL,以及博客文章的類別和其他列表。」

您網站中的每個頁面(包括帖子,頁面,存檔頁面和其他頁面,例如404頁面)都將具有自己的永久鏈接。

因此,例如,您的主頁將位於yoursite.com,而您的博客將位於yoursite.com/blog。

如果您的博客中有「精選」類別,則可以使用以下網址之一:

  • yoursite.com/category/featured
  • yoursite.com/blog/featured或僅
  • yoursite.com/featured。

各個帖子也有自己的固定鏈接。在主題模板文件中,模板標籤the_permalink()將用於獲取帖子的URL並從中創建可點擊的鏈接。

WordPress使用此方法為單個帖子獲取唯一的永久鏈接,並將其輸出到 元素,因此可以將其用作博客文章列表中的鏈接。

擁有此模板標記的好處在於,您只需要使用一段代碼即可獲取指向您網站上任何帖子的鏈接,而無需對您的主題中的任何鏈接進行硬編碼。

永久鏈接,子段和鏈接之間的區別

在這篇文章中,我們將重點關注永久鏈接,但同時我們也會研究。那麼它們之間有什麼區別?

永久鏈接是指向帖子的完整鏈接。因此,我之前在小部件上的Kinsta帖子上給出的鏈接是https://kinsta.com/blog/wordpress-widgets/。

子彈是該永久鏈接的最後一部分,對於該帖子而言是唯一的。在這種情況下,它是wordpress-widgets。

會根據帖子標題為您自動生成該條。而且,如果您想為帖子手動創建一個子彈,則可以。在本文的後面,我將向您展示如何(以及為什麼要這麼做)。

永久鏈接,段,鏈接,URL …這些都是什麼?了解WordPress永久鏈接的來龍去脈⛓

點擊鳴叫

WordPress永久鏈接的創建方式

在靜態網站中,URL通過資源的名稱和目錄路徑標識資源,如以下示例所示:

https://example.com/path/to/resource/wordpress-permalinks.html

為了擁有結構良好的URL,我們只需要一個結構良好的文件系統和正確命名的資源。

但是網路是動態的,我們習慣於使用資料庫驅動的CMS管理網站,這意味著URL將包含許多參數,這些參數的值確定了要針對資料庫運行的查詢。

考慮以下示例:

https://example.com/?key1=val1&key2=val2

在該網址中,您會注意到一個分隔符(問號),以及一組構成查詢字元串的鍵/值對(以&符號分隔)。該URL不滿足可用性和可訪問性要求,應將其轉換為更有意義和SEO友好的永久鏈接。

這些「醜陋的」 URL轉換為優化的永久鏈接的方式取決於您的Web伺服器。如果您是Apache用戶,則需要在根文件夾的.htaccess文件中添加一組重寫指令。如果您是Nginx用戶,則可以在主配置文件中添加一個try_files指令。

但是不用擔心!在大多數情況下,您不需要逐行配置網路伺服器,因為WordPress會為您完成配置。

作為管理員用戶,您可以在管理面板中快速輕鬆地設置自定義重寫規則。高級用戶和開發人員可以受益於WordPress Rewrite API,該API提供的功能和掛鉤可將永久鏈接自定義提升到更高的水平。

WordPress查詢概述

為了構建查詢,執行查詢並存儲WordPress資料庫中的結果,WordPress提供了WP_Query類。多虧了此類,我們不需要關心查詢,因為WP_Query會自動處理請求,構建查詢並執行它。然後,根據模板層次結構,WordPress將返回請求的資源。

WordPress開箱即用,可以接受對單個帖子,頁面,帖子類型以及按類別,標籤,日期,作者等進行排序的大量存檔的請求。

此外,如果默認功能還不夠,開發人員可以通過創建WP_Query類的新實例(查詢對象)或在執行查詢之前將特定參數傳遞給現有查詢實例來構建自定義查詢。

查詢參數被命名為查詢變數,並分為三組。

公共查詢變數

從可以在公共請求(即URL)中使用它們的意義上說,這些變數是公共的。由於這些變數,我們可以要求作者發表文章:

?author = 12?
author_name =米奇

按類別或標籤:

貓= 4、5、6
?category_name = CMS
?tag = wordpress

按日期和時間:

?monthnum = 201601
年= 2015年w = 13
天= 31

按帖子或頁面:

?p = 123
?name = hello-world
?page_id = 234

以及更多。

專用查詢變數

這些變數無意添加到URL查詢字元串中。它們可用於僅在腳本(插件或主題的functions.php文件)中影響查詢。

以下查詢字元串將不會返回預期結果:

?meta_key = city&meta_value =倫敦

meta_key和meta_value是私有查詢變數,不在查詢字元串中定義。它們應該傳遞給查詢對象的實例,如我稍後將向您展示的。

請參見食典中公共和私有查詢變數的完整列表。

自定義查詢變數

這些用戶定義的變數可以通過URL查詢字元串傳遞,就像公共查詢vars一樣。公共變數和自定義變數之間的主要區別是WordPress不會自行處理自定義變數,我們應該從插件獲取它們的值以自定義查詢。

話雖如此,讓我們回到固定鏈接。

醜陋的WordPress固定鏈接和查詢變數

醜陋的永久鏈接顯示查詢字元串,即URL中包含一組查詢變數(查詢字元串)的部分,這些變數將確定返回的資源。

固定鏈接設置屏幕中的普通設置

固定鏈接設置屏幕中的普通設置

例如,考慮以下URL:

https://example.com/?cat=5
https://example.com/?cat=5,7,9

響應這些URL,WordPress將返回屬於指定類別的帖子的存檔。

我們不僅限於每個URL一個參數。在以下示例中,我們將構建更複雜的查詢:

?author_name = lucy&category_name = WebDev?tag = wordpress&m = 201606

在第一個查詢字元串中,author_name和category_name將要求WebDev類別中指定作者的所有帖子。在第二個查詢字元串中,tag和m將要求所有標記為「 wordpress」並於2016年6月發布的帖子。

如您所見,我們可以設置多個查詢變數,並迫使WordPress僅向查詢字元串添加適當的key = value對來運行高級查詢。

漂亮的永久鏈接:更好的選擇

通過啟用Pretty Permalinks,我們設置了一個可用,可訪問且SEO友好的URL結構。讓我們比較以下網址:

https://example.com/?p=123
https://example.com/wordpress-permalinks/

在此示例中,醜陋的永久鏈接顯示p變數及其值(帖子ID),而漂亮URL則顯示帖子段。

WordPress提供了四種Pretty Permalink格式,我們可以在「 Permalink設置」屏幕中進行選擇,如下圖所示。

永久鏈接設置中的漂亮永久鏈接

永久鏈接設置中的漂亮永久鏈接

但您不僅限於默認格式,因為WordPress可讓您通過設置一個或多個結構標籤來自定義漂亮的永久鏈接格式。

自定義結構選項

自定義結構選項

我將在本文的後面詳細介紹這些。

為什麼漂亮的固定鏈接很重要?

為您的WordPress網站使用漂亮的永久鏈接將帶來兩個好處:SEO和用戶體驗。

為什麼?搜索引擎使用您的URL來指示帖子的內容。如果永久鏈接的內容與您的帖子的內容有關,則將有助於搜索引擎確定您的帖子是關於什麼的,以及關於該帖子所聲稱的是合法的。

對於UX,漂亮的永久鏈接會更好,因為它們使用戶更容易記住和使用您網站上的URL。如果是yoursite.com/?p=456,沒有人會記住您的聯繫頁面的URL。但是他們會記住yoursite.com/contact。

固定鏈接,子彈和SEO

帖子條目是帖子URL的最後一部分。如果您配置了WordPress永久鏈接設置,以便使用帖子名稱,則名為「如何創建漂亮的永久鏈接」的帖子的標籤會自動生成為yoursite.com/how-to-create-pretty-permalinks/。

那是一個不錯的子彈。它告訴用戶該帖子的內容,對於搜索引擎來說,它包含「漂亮的永久鏈接」,這可能是您定位的關鍵字。

但是可以改進。

您的子彈應該足夠長,可以包含您要定位的關鍵字,但又要足夠短,以使用戶能夠記住,並且不要使搜索引擎上出現許多不必要的單詞(這裡介紹了如何在WordPress中創建SEO友好的永久鏈接)。

因此,發布一則名為「如何創建漂亮的永久鏈接」的文章可能會更好一些,讓您獲得yoursite.com/pretty-permalinks/。或者,如果您有很多關於永久鏈接的帖子,並且想給此帖子一個具體的提示,請使用create-pretty-permalinks,為您提供yoursite.com/create-pretty-permalinks。

或者,為了進一步發展,您可以通過添加「 WordPress」來進一步提高SEO:example.com/create-wordpress-pretty-permalinks。

當人們在搜索結果中查看您的鏈接時,您也不希望搜索結果太長而無法全部讀取。當我在Google上搜索「 wordpress永久鏈接」時,從Kinsta博客中獲得了以下兩個結果。

Google結果-WordPress永久鏈接

Google搜索結果– WordPress永久鏈接

兩者均具有優化的彈頭。第一個是wordpress-premalinks-url-rewriting,表明它的目標是這些關鍵字,第二個是wordpress-slug,它的重點更加突出。

這些不會浪費任何文字。他們告訴搜索引擎該帖子是關於什麼的,僅此而已。

您可以先通過在「永久鏈接」設置屏幕中選擇「帖子名稱」,然後在撰寫文章時手動編輯每個帖子的標籤來優化SEO的標籤。

永久鏈接,Slug和UX

使用漂亮的永久鏈接和使用簡短而令人難忘的子彈也會為您帶來UX好處。

根據Jacob Nielsen在1999年發布的文章,一個可用的網站要求:

  • 一個易於記憶且易於拼寫的域名。
  • 短網址。
  • 易於輸入的網址。
  • 可視化網站結構的URL。
  • URL是「可入侵的」,允許用戶通過砍掉URL的末尾來轉移到更高級別的信息體系結構。
  • 永久不變的網址。

URL永遠不應更改,因為它可以通過多種方式存儲和共享。這就是我們稱其為永久鏈接的原因。此外,從對非專業用戶立即直觀的意義上來說,URL應該是語義的。

因此,雖然可以在發布後更改帖子的永久鏈接,但這不是一個好主意。這是因為原始永久鏈接可能已經共享。如果您需要更改它,請確保遵循WordPress重定向最佳做法。

如何在WordPress中更改固定鏈接設置

在WordPress中,您可以通過多種方式更改永久鏈接:

  • 您可以編輯「永久鏈接」設置屏幕以打開漂亮的永久鏈接-在設置網站後應立即執行的操作。
  • 您可以在「永久鏈接」屏幕中編輯標籤和類別的永久鏈接結構。
  • 您可以在創建和編輯單個帖子時編輯它們。
  • 註冊自定義帖子類型時,可以指定其鏈接的結構,選擇使用默認值或替代默認值。
  • 您可以編寫一個插件來修改永久鏈接的結構方式。
  • 您可以使用重定向獲取指向新鏈接的過期永久鏈接。

讓我們來看看這些。

使用WordPress,我們的流量增長了1,187%。
我們將向您展示如何。
加入20,000多個其他人,他們每周都會收到有關WordPress內部技巧的新聞!

          現在訂閱
        
        
          
            
            成功!感謝您的訂閱

您將在一周內收到下一期的Kinsta新聞通訊。

訂閱Kinsta新聞通訊
        
  
    
      
    
      
        訂閱
    
  
  
    

我同意條款和條件以及隱私政策

編輯整體固定鏈接設置

永久鏈接設置屏幕是配置永久鏈接的第一個地方。通過設置>永久鏈接訪問它。

固定鏈接設置屏幕

固定鏈接設置屏幕

常用設定

第一部分介紹單個帖子的設置。選項包括:

  • 普通:將帖子ID用於鏈接。這對瀏覽器有意義,但對人類或搜索引擎而言意義不大。看起來像這樣:example.com/?p=123。
  • 日期和名稱:包括發布日期的完整日期及其名稱(或更準確地說,其子句)。看起來像這樣:example.com/2020/06/01/my-post/。
  • 月和名稱:這是日期和名稱的縮寫,僅包含年份和年份,而不是日期:example.com/2020/06/my-post/。
  • 數字:與普通選項一樣,它使用帖子ID,並且對用戶不太友好。 example.com/archives/123。
  • 帖子名稱:此選項不包含任何日期或帖子ID,而僅使用以下代碼:example.com/my-post/。
  • 自定義結構:您可以在此處創建自己的自定義結構。使用標籤可根據帖子數據獲取信息,並使用靜態文本添加帖子之間不會更改的內容。

這些標籤是包裝在%字元中的特定關鍵字。 WordPress提供以下標籤:

  • %year%–發布後的年份(四位數)。
  • %monthnum%–發布的月份(兩位數字)。
  • %day%–發布日期(兩位數)。
  • %hour%–發布的小時數(兩位數)。
  • %minute%–發布的分鐘數(兩位數字)。
  • %second%–發布的秒數(兩位數)。
  • %post_id%–帖子唯一ID(整數)。
  • %postname%-帖子標題(即表示帖子標題的經過凈化處理的字元串)。
  • %category%–類別標籤。
  • %author%–作者。

嘗試檢查「自定義結構」單選按鈕,並將以下字元串之一添加到文本欄位中:

  • /%author%/%postname%/
  • /%year%/%postname%/
  • /%category%/%postname%/

這些字元串中的任何一個都會生成具有特定語義值的不同的漂亮永久鏈接,如下所示:

example.com/rachelmccollin/wordpress-permalinks/
example.com/2020/wordpress-permalinks/
example.com/CMS/wordpress-permalinks/

在第一個示例中,結果URL突出顯示了帖子的作者。其他兩種格式分別告訴我們出版的年份和職位的類別。選擇最適合您的格式由您決定。

選擇所需選項後,請轉到「可選」部分,或單擊「保存更改」以保存設置。

可選的永久鏈接設置

除了單個帖子的設置,「永久鏈接設置」屏幕還允許您為類別和標籤檔案設置自定義結構。

如果您不這樣做,則默認為在永久鏈接的末尾包含/ category / category-slug /。因此,如果您的類別為「精選」,則其存檔頁面將位於yoursite.com/category/featured。

可選的永久鏈接設置

可選的永久鏈接設置

您可以在「永久鏈接」設置頁的「可選」部分中對此進行修改。因此,如果您希望將yoursite.com/blog/featured/作為該類別存檔的永久鏈接,則可以在「類別」基礎欄位中輸入博客。您不必插入反斜杠或使用標籤。

如何更改單個帖子和頁面的永久鏈接和標籤

在WordPress網站上激活漂亮的永久鏈接後,就該對單個帖子和頁面進行優化了。

最好在創建內容時執行此操作。如果您更改帖子的內容,則將更改其使用的URL,並且您或您的訪客過去共享的任何鏈接將不再起作用。

要編輯信息,請在該信息的信息編輯屏幕中進行。轉到帖子,然後選擇要編輯的帖子。 (如果您正在創建帖子,則已經在右側屏幕上。)

在帖子編輯屏幕中,選擇右側的「文檔」窗格,然後轉到「永久鏈接」部分。如果尚未打開,請單擊其右側的箭頭。

在帖子編輯屏幕中進行永久鏈接編輯

在帖子編輯屏幕中進行永久鏈接編輯

自動生成的Slug將顯示在URL Slug欄位中。您可以對其進行編輯,以使子彈更短,更集中。

在編輯它之前,請將舊的塊複製到某個地方,以便以後需要設置重定向時可以使用它(這僅適用於以前發布的帖子)。

子彈頭

子彈頭

現在,單擊「發布」或「更新」按鈕以保存更改。

別忘了:如果您為現有帖子編輯了標籤,則可能會對擁有原始鏈接並應使用重定向的用戶造成麻煩。

如何更改存檔頁面的永久鏈接設置

要更改單個存檔頁面的永久鏈接設置,您可以在「永久鏈接」設置屏幕中編輯「類別」或「標籤」庫的設置。您還可以更改單個類別,標籤或自定義分類法的標記。

讓我們看一下您的操作方法,然後繼續編輯自定義分類法的永久鏈接,並在註冊時輸入文章類型。

更改類別和標籤的子彈

為此,請轉到帖子>類別(或帖子>標籤)。

類別編輯畫面

類別編輯畫面

找到您要編輯其類別的類別或標籤,然後單擊其名稱。

編輯類別標籤

編輯類別標籤

然後,您可以輸入類別或標籤的標籤。 WordPress會根據類別或標籤的名稱自動生成一個,但您不必保留它。與帖子一樣,在設置類別或標籤時也可以這樣做。如果以後再做,則需要設置重定向。

如果您設置了一種自定義分類法,或者是由插件創建的,則可以按照完全相同的方式來編輯該分類法中各個術語的標籤。但是,如果您要為分類法本身編輯子彈,則需要編輯一些代碼。

更改自定義分類法的子彈

當您註冊定製分類法時,該分類法的存檔頁面將自動具有yoursite.com/taxonomy/term的URL,其中分類法是分類法ID,術語是術語slug。

假設您為ID為kinsta_language的語言註冊了自定義分類法,該分類法使用前綴來確保與其他插件註冊的任何其他分類法不同。然後,您可能會用法語創建一個術語。

該分類術語的存檔的URL為yoursite.com/kinsta_language/french。

但是,如果您想更改它以使其不包含該前綴並且更加用戶友好,該怎麼辦?註冊分類法時,可以使用rewrite參數執行此操作。

這是用於註冊分類法的代碼,包括rewrite參數。

函數kinsta_register_taxonomy(){

//語言
$ labels =數組(
‘name’=> __(’Languages’),
  ‘singular_name’=> __(’Language’),
  ‘search_items’=> __(’Search Languages’),
  ‘all_items’=> __(’All Languages’),
  ‘edit_item’=> __(’Edit Languages’),
  ‘update_item’=> __(’更新語言’),
  ‘add_new_item’=> __(’添加新語言’),
  ‘new_item_name’=> __(’新語言名稱’),
  ‘menu_name’=> __(’Languages’),
 );

$ args =數組(
  ‘標籤’=> $ labels,
  ‘hierarchical’=>是,
  ‘sort’=>是,
  ‘args’=> array(’orderby’=>’term_order’),
  ‘rewrite’=> array(’slug’=>’language’),
  ‘show_admin_column’=>是,
  ‘show_in_rest’=>是
 );

register_taxonomy(’kinsta_language’,array(’post’,’attachment’),$ args);

}

add_action(’init’,’kinsta_register_taxonomy’);

該代碼中的重要一行是這一行:

‘rewrite’=> array(’slug’=>’language’),

這會將該段從kinsta_language(ID)重寫為語言(新值)。因此,您的新URL為yoursite.com/language/french。更人性化!

如何更改自定義帖子類型的子彈

自定義帖子類型的註冊方式與自定義分類法相同,因此它們的URL包含自定義帖子類型的ID。

想像一下,您註冊了一個名為kinsta_book的自定義帖子類型,並創建了一個名為“Huckleberry Finn”的帖子類型,其中包含一束huckleberry-finn。

該網址為yoursite.com/kinsta_book/huckleberry-finn。帖子類型存檔的條目將是yoursite.com/kinsta_book。

同樣,您可以在註冊自定義帖子類型時使用rewrite參數更改此設置。同樣,這是包含該參數的代碼:

函數kinsta_register_post_type(){

 //書籍
 $ labels =數組(
  ‘名稱’=> __(’書籍’),
  ‘singular_name’=> __(’Book’),
  ‘add_new’=> __(’New Book’),
  ‘add_new_item’=> __(’添加新書’),
  ‘edit_item’=> __(’Edit Book’),
  ‘new_item’=> __(’New Book’),
  ‘view_item’=> __(’View Book’),
  ‘search_items’=> __(’Search Books’),
  ‘not_found’=> __(’No Books Found’),
  ‘not_found_in_trash’=> __(’在垃圾箱中沒有找到書’),
 );

 $ args =數組(
  ‘標籤’=> $ labels,
  ‘has_archive’=>是,
  ‘public’=>是,
  ‘hierarchical’=>否,
  ‘支持’=>數組(
   ‘標題’,
   ‘編輯’,
   ‘摘抄’,
   「自定義欄位」,
   「縮略圖」,
   「頁面屬性」
  ),
  ‘taxonomies’=> array(’kinsta_language’,’category’),
  ‘rewrite’=>數組(’slug’=>’book’)
 );
 
 register_post_type(’kinsta_book’,$ args);

}

add_action(’init’,’kinsta_register_post_type’);

子彈的重要一行是這一行:

‘rewrite’=>數組(’slug’=>’book’)

因此,現在一本單獨的書的URL將是yoursite.com/book/huckleberry-finn,而該檔案庫的URL將是yoursite.com/book。

信息

談論書籍……您是否已經檢查過Kinsta的電子書部分?它們是免費下載的,並且包含所有可行的提示!

使用自定義欄位編輯永久鏈接

除了公共和私有查詢變數外,WordPress還允許開發人員和高級用戶定義自己的自定義查詢變數。一旦註冊,這些變數就可以像公共查詢變數一樣添加到查詢字元串中,並且它們的值也可以用於影響查詢。

以下是利用自定義查詢變數創建自定義元查詢(即通過自定義欄位檢索帖子的查詢)的方法。

為了實現此目標,我們將開發一個插件,通過該插件我們可以註冊自定義變數,獲取它們的值並相應地更改查詢。

就是這樣…

在wp-content / plugins目錄中創建一個插件。添加一個函數來註冊查詢變數:

/ **
*註冊自定義查詢變數
*
* @param array $ vars可用查詢變數的數組
* /

函數myplugin_register_query_vars($ vars){

 $ vars[] =’城市’;
 返回$ vars;

}

add_filter(’query_vars’,’myplugin_register_query_vars’);

使用query_vars過濾器,您可以在執行查詢之前添加,刪除或更改公共查詢var。示例中的回調函數將可用變數的數組存儲為參數,以添加新變數並返回相同的數組。

接下來,添加此函數,該函數使用變數的值來更改查詢:

/ **
*建立自定義查詢
*
* @param $ query obj WP_Query實例(通過引用傳遞)
*
* /

函數myplugin_pre_get_posts($ query){

 //檢查用戶是否正在請求管理頁面
 //或當前查詢不是主要查詢
 如果(is_admin()||!$ query-> is_main_query()){
  返回;
 }

 $ city = get_query_var(’city’);

 //添加meta_query元素
 if(!empty($ city)){
  $ query-> set(’meta_key’,’city’);
  $ query-> set(’meta_value’,$ city);
  $ query-> set(’meta_compare’,’LIKE’);
 }

}

add_action(’pre_get_posts’,’myplugin_pre_get_posts’,1);

在創建查詢之後但在執行查詢之前,會觸發pre_get_posts操作掛鉤。因此,我們可以將回調函數連接到該操作,以在查詢運行之前對其進行更改。就是這樣:

  • 回調函數保留$ query對象的實例,該實例通過引用而不是通過值傳遞。這意味著對查詢對象的任何更改都會影響原始查詢,而不是原始副本。因此,我們必須確定要執行哪個查詢(主查詢)。
  • 稍後,藉助get_query_var函數,我們可以從當前查詢字元串中獲取城市值。
  • 最後,如果$ city不為空,我們可以設置元查詢元素meta_key,meta_value和meta_compare。後者是私有查詢變數,不適用於公共請求。它們的值只能在腳本中設置。

現在激活插件,將城市自定義欄位添加到許多帖子中。轉到「設置」>「永久鏈接」以刷新永久鏈接,實際上您無需執行任何操作;只需訪問屏幕就足夠了。

現在檢查如下URL:

https://example.com/?city=倫敦

響應此請求,WordPress將返回城市欄位值為倫敦的所有帖子。

我們的最後一個任務是以漂亮的永久鏈接結構轉換上面示例的醜陋URL。讓我們在插件中添加以下功能:

/ **
*添加重寫標籤和規則
* /

函數myplugin_rewrite_tag_rule(){

 add_rewrite_tag(’%city%’,’([^&]+)’);
 add_rewrite_rule(’^ city /([^/]*)/?’,’index.php?city = $ matches[1]’,’最佳’ );

}

add_action(’init’,’myplugin_rewrite_tag_rule’,10,0);

add_rewrite_tag和add_rewrite_rule函數是重寫API的一部分。 add_rewrite_tag使WordPress知道城市查詢變數,而add_rewrite_rule指定新的重寫規則。這兩個函數都應該與init操作掛鉤。由於有了新的標記和規則,我們可以使用以下URL:

https://example.com/city/倫敦/

WordPress將返回帖子的檔案,其中城市自定義欄位值為倫敦。

注意:每當添加新的重寫規則時,都必須從「設置」管理菜單下的「永久鏈接」屏幕刷新WordPress永久鏈接。

如何在WooCommerce中更改永久鏈接

WooCommerce創建自己的自定義帖子類型和分類,所有這些都有由插件定義的默認永久鏈接。

您可以為所有這些編輯永久鏈接設置和段。

更改產品類別,標籤和屬性永久鏈接

編輯產品類別,標籤和屬性的永久鏈接有兩個方面:結構和彈頭。它們的工作方式與常規類別和標籤類似。

要編輯永久鏈接結構,請轉到「設置」>「永久鏈接」,然後找到「可選」部分,WooCommerce將在其中添加一些其他欄位。

安裝了WooCommerce的可選永久鏈接設置

安裝了WooCommerce的可選永久鏈接設置

在這裡,您可以編輯WooCommerce添加的三個自定義分類法的永久鏈接設置:

  • 產品類別:默認為/ product-category /,但是如果您在商店中使用其他術語,則可以對其進行修改。確保所做的更改不會與常規類別的設置衝突,因為常規類別的設置不是同一回事。
  • 產品標籤:默認為/ product-tag /,您可以根據需要進行更改。確保避免與常規帖子標籤發生衝突。
  • 產品屬性:它們的工作方式不同於其他兩個分類法,並且具有不同的結構。無論您在此處添加的內容如何,​​都將始終跟隨各個屬性名稱(例如大小)和屬性本身(例如術語「大」)的標記。

如果要編輯單個類別或標籤的段,請轉到「產品」>「類別」(或「產品」>「標籤」),然後以與發布標籤和類別相同的方式進行編輯。

產品類別標籤編輯

產品類別標籤編輯

編輯屬性是不同的,因為您不僅具有屬性本身,而且具有屬性術語。

首先轉到產品>屬性。

產品屬性屏幕

產品屬性屏幕

創建新屬性時,可以使用「子彈」欄位以與標記或類別相同的方式設置子彈。或者,要編輯現有屬性的段,請在右側列表中單擊該屬性下方的「編輯」鏈接。

編輯產品屬性段

編輯產品屬性段

單擊更新以保存您的更改。

要編輯屬性詞條,請轉到屬性屏幕,然後單擊屬性旁邊的「配置條件」鏈接。這將帶您到該屬性的術語列表。

產品屬性條款列表

產品屬性條款列表

現在,就像編輯類別或標籤一樣,編輯該詞條。然後,將其添加到具有該術語的產品的存檔URL中。

是否需要為您的新網站提供快速,可靠且完全安全的託管服務? Kinsta提供了所有這些以及WordPress專家提供的24/7世界一流的支持。查看我們的計劃

更改產品永久鏈接

您可以通過「設置」>「永久鏈接」來編輯產品永久鏈接。向下滾動到產品永久鏈接部分。

產品永久鏈接設置

產品永久鏈接設置

在這裡,您可以從產品的四個永久鏈接設置中進行選擇:

  • 默認值:如果您已經激活了漂亮的永久鏈接,則將每個產品的標籤與/ product /一起使用。
  • 店鋪:您的商店將使用/ shop /,而不是使用/ product /。
  • 帶有類別的商店:將當前產品的類別插入URL。如果您的產品類別反映了這些關鍵字,則可能會添加您要定位的關鍵字,但由於它會創建長網址,因此對用戶體驗沒有幫助。
  • 自定義基礎:使用適用於您商店的字詞創建您自己的URL結構。您無法完全移除底座,必須使用某些物品。

Once you』ve chosen the option you want, click the Save Changes button to save your choice.

You can also edit the slug for an individual product in the product editing screen, in exactly the same way you would for a post or page.

How to Change WordPress Permalinks With a Plugin

You can use a third party plugin to make amendments to your permalink settings over and above what the default WordPress Permalinks settings screen lets you do.

Custom Permalinks WordPres plugin

Custom Permalinks WordPres plugin

  • The Custom Permalinks plugin lets you set the URL of any post category or tag to whatever you want. It also sets up redirects so the old URLs will still work.
  • The Permalink Manager Lite plugin supports custom post types as well as custom taxonomies in the pro version. It also includes redirects and works with third-party plugins such as WooCommerce and Yoast.

How to Change WordPress Permalinks in phpMyAdmin

If you know what you』re doing and are sure you won』t break anything, you can also edit permalinks in phpMyAdmin.

This is something you might need to do if you can』t access the Permalinks settings screen for any reason.

Start by backing up your database. You』re going to be directly editing it so it』s important to back up in case you make a mistake.

Access phpMyAdmin.

If you』re a Kinsta customer, you do this by logging in to MyKinsta and then selecting the site you want to work with.

Scroll down in the Info screen and click the Open phpMyAdmin button.

Open phpMyAdmin in MyKinsta

Open phpMyAdmin in MyKinsta

Type in your database username and password to access phpMyAdmin. You can retrieve these from the Info screen.

Click on the Databases tab at the top and then select the database you want to work with.

Database structure in phpMyAdmin

Database structure in phpMyAdmin

Select the wp_options table and find the permalink_structure entry in the option_name column. You might need to navigate beyond the first page of entries.

Finding the permalink_structure entry

Finding the permalink_structure entry

Click the Edit link on the left for that entry then under the option_value field, add the permalink structure that you want to use. Use the tags that we identified earlier for use in the Permalinks settings screen.

Editing the permalink structure

Editing the permalink structure

Click Go. Now your permalinks will be updated.

Working with Image Permalinks

Images have permalinks all of their own, and each image or media file you upload to your site will have a number of links created for it:

  • The link to the image you uploaded – the original image.
  • Links for the new images that are generated using the media sizes you set via Settings > Media.

The Link to the Original Image

When you upload an image, a unique link will be created to the file where it is stored on your server. This will include the path to where it is kept, which is wp-content/uploads.

It will also include the date in which you uploaded the image. This means that if you upload another image with the same file name next month (or next year), the images won』t be confused as they』ll have unique file paths.

If you upload an image called funnycat.jpg on 1 April 2020, its link will be yoursite.com/wp-content/uploads/04/funnycat.jpg. The 04 indicates that the file was uploaded in April. WordPress creates a numbered folder in the uploads directory for each month.

If you uploaded a file that wasn』t an image, the URL would work in the same way: yoursite.com/wp-content/uploads/04/document.pdf.

If you upload more than one file with the same name in a given month, WordPress will append a number to the end of the file name. So if I upload another image called funnycat.jpg, it will call it funnycat-1.jpg.

If you ever need to link to the original image or find it to check it』s working correctly, this is how you find the link.

You can also find the link to the attachment file by going to Media > Library and clicking on the file. The editing screen will be displayed for that file and you can find its URL in the File URL field on the right-hand side.

Original image link

Original image link

You can also link to the file using the wp_get_attachment_image() function provided by WordPress. This is a better practice as it means the link won』t change if the attachment is moved in the future. This is a function you would use in a plugin or a theme template file, and it uses the unique ID of the attachment file.

In the case of my funnycat.jpg image, the ID is 4995. I can get this by going to the image editing screen and clicking on the URL for this screen at the top of the browser window. The final digits will be the ID.

To fetch this image in a template file or plugin, I would use this code:

This would fetch the full-size image. If I wanted to output it I would add echo:

Permalinks to Different Sized Images

WordPress will also create images using the file size settings that have been configured for your site. You do this by going to Settings > Media.

Media settings screen

Media settings screen

So if your image is already bigger than the large setting, it will create three images – large, medium, and thumbnail.

It doesn』t name them using these conventions because you might change the setting for those in the future. Instead, it uses the dimensions of the file within the filename and saves them to the same place as the original image, in the month folder in wp-content/uploads.

The easiest way to find the links is to look at your FTP client and find all of the images uploaded in a given month.

Uploaded images in FTP client

Uploaded images in FTP client

Let』s take the image I』ve uploaded to my site called funnycat.jpg. You can see it in the screenshot above.

WordPress has also created extra files using the file size settings for my site:

  • funnycat-150×150.jpg
  • funnycat-222×300.jpg
  • funnycat-300×200.jpg
  • funnycat-757×1024.jpg
  • funnycat-768×1040.jpg
  • funnycat-1135×1536.jpg
  • funnycat-1513×2048.jpg

There are more than the standard ones there because I』m using plugins that make use of extra image sizes and have set an additional custom size for one of my page templates in the theme. But you could use the link to any of these as a hard link if you wanted to.

However a better option if you wanted to link to the image, would be to use the wp_get_attachment_image() function we』ve already looked at, and add an extra parameter for the image size.

So to output the medium-sized image, you would use this:

That』s a much more robust way of fetching the image than creating a hard link in your code.

How to Redirect Permalinks for Different Content Types

Editing the slug for an existing post, or changing the permalink settings overall, might cause a problem if you』ve previously shared posts using the old links. If someone then clicks on those links, they』ll be taken to a 404 page.

You can fix this by creating redirects from the old links to the new ones.

Redirecting Individual Posts and Pages

To redirect the old slug from a post to the new one, you』ll need to set up a redirect rule for those two URLs.

If you』re with Kinsta, you can create redirect rules in the MyKinsta dashboard.

Find your site, and then click the Redirects option in the menu.

Redirects in MyKinsta

Redirects in MyKinsta

Click the Add redirect rule button to open the redirect rules popup.

MyKinsta add redirect rule

MyKinsta add redirect rule

To add your redirect, select 301 redirect, then type in the old slug as the Redirect from value and the new slug as the Redirect to value.

Click the Add redirect rule button and your redirect will be set up.

If you aren』t with Kinsta, you can use a redirection plugin to set up redirects. The Redirection plugin is the most popular. It will let you set up redirects manually and it will also monitor changes to your slugs and automatically set up redirects for you.

Below you can see that I』ve changed the slug for one of my posts and the plugin has caught it and set up a redirect rule.

Redirection plugin redirect rule set up

Redirection plugin redirect rule set up

Redirecting Archive Pages

If you change the structure of your archive pages using the Optional section in the Permalinks settings page, anyone using the old link to a category archive will be taken to your 404 page. So you』ll need to set up a wildcard redirect.

In MyKinsta, create a redirect rule that uses the base structure you were using before and the one you』re using now, with an asterisk to denote a wildcard after it.

In the Redirect from field, type the old path to categories, with a wildcard. It needs to take the form /oldslug/(.*)$. The entry for Redirect to needs to take the form /newslug/$1.

So if you』ve changed your category URL structure to use blog before the category name instead of the default category, you would enter /category/(.*)$ in the Redirect from field and /blog/$1 in the Redirect to field.

Adding a wildcard redirect in MyKinsta

Adding a wildcard redirect in MyKinsta

If you』re using the Redirection plugin, you』ll need to enable Regex functions first, as the wildcard asterisk is a regex function.

Go to Tools > Redirection and go to Add new redirection section of the screen.

Creating a new redirect rule with the redirection plugin

Creating a new redirect rule with the redirection plugin

Click on the URL Options/Regex drop down and check the Regex box.

Setting up a wildcard redirect with the Redirection plugin

Setting up a wildcard redirect with the Redirection plugin

In the Source URL field, type the old path to categories, with a wildcard. It needs to take the form /oldslug/(.*)$. The entry for Target URL needs to take the source /newslug/$1. This works in exactly the same way as in MyKinsta.

Troubleshooting WordPress Permalinks

Occasionally you might find that permalinks don』t work as you expect them to. Here』s what to do if that happens.

Permalinks Not Working After Registering a Post Type

Sometimes you register a new post type or taxonomy and the links to the relevant archive pages or to posts of the post type don』t worry.

Don』t panic! This is simply because WordPress doesn』t know that the custom post type or taxonomy means a change to permalink settings. Simply go to Settlings > Permalinks to refresh settings. You don』t even need to make any changes or click the Save Changes button – just opening the screen will be enough.

Permalinks Not Behaving the Way You Want Them To

If your permalinks aren』t working the way you expect and it isn』t because you just registered a custom post type or taxonomy, try these tips.

  • Check that you typed in the URL correctly in the browser.
  • Go to Settings > Permalinks and check the setup. Are the tags correct? Make sure you haven』t missed anything or used the wrong syntax.
  • Clear your website』s cache if you』re using a caching plugin. Caching can interfere with changes to links.
  • If you』ve changed permalinks settings, you might have links in the body of your website content that are now out of date. Edit them or set up redirects.
  • Check if you have manually changed the slug for any posts and if these need editing.
  • Check whether you have a redirection plugin installed or a plugin that includes redirection. If so, check what redirects you』ve added to the settings.
  • If you have any plugins that affect post types or links, try deactivating them and seeing if that fixes the problem.
  • If you can』t access the Permalinks settings screen, use the method detailed earlier to edit permalinks via phpMyAdmin.

Follow these tips and you should be able to get your permalinks working as they should.

Permalinks are the building blocks of any WordPress site. Learn what they are, how they work, and how to optimize them for better SEO traffic ?

Click to Tweet

摘要

Permalinks are an incredibly useful feature of WordPress. You can use them to enhance user experience and boost your search engine rankings.

If you follow the guide above, you』ll have permalinks that are optimized. You』ll be able to configure them to work in exactly the way you need them to, for all post types, taxonomies, and custom slugs.

Now, it』s your turn: how do you manage your permalinks? Did we forget to cover anything about WordPress permalinks?在下面的評論中讓我們知道!

If you enjoyed this article, then you』ll love Kinsta』s WordPress hosting platform. Turbocharge your website and get 24/7 support from our veteran WordPress team. Our Google Cloud powered infrastructure focuses on auto-scaling, performance, and security. Let us show you the Kinsta difference! Check out our plans

相關文章