wordpress - 木木如何实现get_current_url wp

标签 wordpress twig timber

我使用 Wordpress 已经一年多了。但我一直坚持使用 Timber twig 框架获取当前 URL。我在下面的代码中尝试了这些代码,但没有成功。

{{ site.url.current }}
{{ app.request.getRequestUri() }}

Twig templates engine: get current url

最佳答案

你试过吗:

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/

相关文章:

php - 在木 Twig 文件内循环

php - Twig_Error_Syntax for "Unknown filter"与 Timber 中的 Twig 过滤器

wordpress - "Add New"插件按钮未出现在 WordPress 管理面板上

php - 当我使用 get_query_var 时看到的随机字符串是什么?

html - 当我有分层背景时,如何在内容周围创建填充?

php - 使用 twig 创建即用型共享元素

wordpress - 木材 - 列出类别及其帖子

html - Wordpress 中的 rel ="generator"标签是什么意思?

symfony - Knp DoctrineBehaviors 抛出 ContextErrorException : Warning: call_user_func_array() expects parameter 1 to be a valid callback

wordpress - 如何用twig和wmpl判断语言