所以这是我的问题。 Wordpress PHP功能
the_permalink();
给我这个网址:http://www.website.com/author-20/article-title
我基本上需要他的父URL。我怎么才能得到它? (http://www.website.com/author-20/)
如果链接始终以这种方式构建,则可以在最后一个/
之后剪切该部分。在PHP中这样做的方法是使用函数substr
和strrpos
。
$parentUrl = substr($permaLink, 0, strrpos($permaLink, '/'));
substr
切出一部分字符串,从第二个参数开始,第三个参数的长度。
strrpos
搜索字符串中字符的最后位置。
如果链接结构表示后父结构,请考虑this question from the wordpress stackexchange community。
你可以使用dirname()
function。
$url = "http://www.website.com/author-20/article-title";
var_dump(dirname($url));
输出:
http://www.website.com/author-20