我使用 Wordpress 已经一年多了。但我一直坚持使用 Timber twig 框架获取当前 URL。我在下面的代码中尝试了这些代码,但没有成功。
{{ site.url.current }}
{{ app.request.getRequestUri() }}
最佳答案
你试过吗:
URLHelper::get_current_url()
文档:https://timber.github.io/docs/reference/timber-urlhelper/#get_current_url
因此,您应该能够将其作为变量提供到您的模板中。
或者,如果您想更进一步,并且 extend Timber's Twig即创建一个过滤器或函数,如:
$twig->addFilter(new \Twig_SimpleFilter('is_current_url', function ($link) {
return (URLHelper::get_current_url() == $link) ? true : false;
}));
这应该归结为:
{{ 'http://example.org/2015/08/my-blog-post' | is_current_url }}
顺便说一句:在内部,get_current_url()返回:$_SERVER['HTTP_HOST']
/$_SERVER['SERVER_NAME']
+ $_SERVER["REQUEST_URI"]
关于wordpress - 木木如何实现get_current_url wp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64921780/