2016-10-01 113 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

没关系。总的错字。多数民众赞成在深夜编程没有咖啡:) –