dongtuo8170 2016-10-01 08:33
浏览 33

Wordpress条件永久链接

I have a specific page on my wordpress website example.com/download/ that I already have a page for. I would like to add some sort of php code to enable the following functionality.

  • If there is a subpath after the /download/<name>, I would like to fetch a file from some directory based on <name>
  • If no subpath is specified (/download/) I want to display the page that is already written

Is this possible with Wordpress hooks/filters?

I have a feeling it has something to do with the WP_Rewrite class, but I am not sure exactly what to write, nor where to put it without WP or theme updates wiping the code

  • 写回答

1条回答 默认 最新

  • du77887 2016-10-01 09:16
    关注

    Yes, you have to add a custom rewrite rule. You can use your theme's functions.php to add the new functionality.

    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);
    

    To make it work, first you need to go to Admin -> Settings -> Permalinks and click the save button so the new rule is added.

    评论

报告相同问题?