让我们把这些函数翻译成英语,然后按运行顺序排列。在这个回答中,我将有意避免使用PHP,但必须对PHP有基本的了解:
When the `post_type_link` filter runs: // the add_action calls
run: `wpa_course_post_link` as it has priority 3 // function wpa_course_post_link(....
if the object is a post
and that post has a course term
search for %course% in the URL string and replace it
Then, run `custom_post_permalink` as it has priority 10: // function custom_post_permalink(...
Ignore the URL we got given and construct a brand new one by returning the post_type + "/" + posts name + ".html"
所以
custom_post_permalink
不添加
.html
在URL的末尾,它会创建一个全新的URL。如果您将优先级从3更改为11,则您的。html URL可以工作,但您的课程URL不会
%course%
已替换。
幸运的是wpa_course_post_link
函数为如何执行此操作提供了更好的模板。与其抓住课程术语并替换字符串,只需添加即可。html到$post_link
字符串并返回它
因此,如果我们用简单的英语将其写成伪代码,我们可能会得到以下结果:
When the `post_type_link` filter runs:
run: `wpa_course_post_link` as it has priority 3
if the object is a post, then:
if that post has a course term then:
search for %course% in the URL string and replace it
then add ".html" to the end
我将转换为PHP作为读者的一项任务,问题中已经提供了所有需要的PHP。