WordPress 中的 $GLOBALS 变量用于跨模板访问值?
在 WordPress 中,有没有办法将值(特别是永久链接 URL 字符串)存储在我可以跨模板文件访问的变量中?
我需要做的是:在我的 WordPress 单页面模板之一中,我需要创建一个指向将用户带到那里的源页面的链接。
由于网站上有许多不同的页面可以将用户发送到此页面,因此我想到的解决方案是将源页面的永久链接存储在某种可以由目标访问的全局变量中页面模板。这将允许我创建一个返回源页面的链接。
我在谷歌上搜索解决方案并找到了一个: 在我的每个源页面上,我这样做:
$GLOBALS['my_variable'] = get_permalink($post->ID);
然后在目标页面模板上,我这样做:
<a href="<?php echo $GLOBALS['my_variable']; ?>">Back</a>
但这不起作用。该变量在源页面模板中被正确设置,但在目标页面上,不知何故该变量最终每次都会被该(目标)页面的永久链接替换。我不明白为什么。请一些帮助!
In wordpress, is there a way to store a value (specifically, a permalink URL string) in a variable that I can access across template files?
What I need to do is this: in one of my wordpress single page templates, I need to create a link to the source page that brought the user there.
Since there are a number of different pages across the site that could have sent the user to this page, the solution that occurred to me was to store the permalink of the source page in some sort of a global variable that could be accessed by the destination page template. This would allow me to create a link back to the source page.
I googled for solutions and found one: On each of my source pages, I do this:
$GLOBALS['my_variable'] = get_permalink($post->ID);
and then on the destination page template, I do this:
<a href="<?php echo $GLOBALS['my_variable']; ?>">Back</a>
But this doesn't work. The variable gets set correctly in the source page template, but on the destination page, somehow the variable ends up getting replaced by the permalink of that (destination) page every time. I can't figure out why. Some help please!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将其保存在
$_SESSION
中吗?You could just save it in the
$_SESSION
?您可以使用 Transient API 来执行此类操作。
You can use the Transient API to do that sort of thing.