2016-10-01 128 views
0

我在我的wordpress網站example.com/download/上有一個特定的頁面,我已經有了一個頁面。我想添加一些PHP代碼來啓用以下功能。WordPress有條件的固定鏈接

  • 如果在/download/<name>後的子路徑,我想獲取基於<name>
  • 某些目錄中的文件如果沒有指定子路徑(/download/)我想顯示已經寫入
  • 頁面

這是可能與WordPress的掛鉤/過濾器?

我有一種感覺它有事情做與WP_Rewrite類,但我不知道到底要寫什麼,也沒有把它放在哪裏沒有WP或主題更新擦碼

回答

1

是的,你必須添加一個自定義重寫規則。你可以使用你的主題的functions.php來添加新的功能。

function custom_rewrite_rule() { 
    add_rewrite_rule('^download\/([^/]+)\/?','your_url_to_the_downloader_file.php?download=$matches[1]','top'); 
} 
add_action('init', 'custom_rewrite_rule', 10, 0); 

要使其工作,首先你需要去Admin -> Settings -> Permalinks並單擊因此添加新規則的保存按鈕。

+0

正是我在找的東西。謝謝 –

+0

試圖合併此。我收到一個錯誤'function custom_rewrite_rule(){add_rewrite_rule('^ download \ /([^ /] +)\ /?','/ wp-content/download_content/$ matches [1]','top'); } add_action('init','custom_rewrite_rule',10,0); 警告:無法修改標頭信息 -/home/content/20中已經發送的標頭(輸出開始於/home/content/20/10282520/html/wp-content/themes/theme-child/functions.php:4) /10282520/html/wp-includes/pluggable.php 1203行# –

+0

沒關係。總的錯字。多數民衆贊成在深夜編程沒有咖啡:) –