gpt4 book ai didi

php - 在 Magento 中获取当前 URL 并显示一些东西

转载 作者:IT王子 更新时间:2023-10-29 01:14:43 25 4
gpt4 key购买 nike

我正在尝试获取 Magento 中的当前 URL 并在我当前位于该页面上时显示一些内容。到目前为止,这就是我所做的并且有效。

 <?php
$currentUrl = $this->helper('core/url')->getCurrentUrl();
?>

<?php if($currentUrl === 'http://powerplantv2.jehzlau.net/blog') { ?>I am in the blog page<?php } ?>

但是,我不想在源代码中硬编码url,因为如果我转移到另一个服务器,我需要重新修改phtml文件。

我尝试了在网上找到的所有方法,但没有用。我希望这里的一些 Magento 专家可以启发我我做错了什么。 :(

最佳答案

您可以通过执行以下操作来检索当前 URL 路径:

$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
$path = $url->getPath();

然后使用一些基本逻辑,您可以定位到 /blog 页面。

$blogPaths = array('/blog', '/blog/', '/index.php/blog/');
if(in_array($path, $blogPaths))
{
//Do something on /blog
}

关于php - 在 Magento 中获取当前 URL 并显示一些东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25192212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com