因为Joomla 2.5没有提供规范URL的插件(除了SEF404),我将尝试"破解"我的模板,并在需要时包含一个规范URL。
因为每个菜单项都可以通过不同的URL和get参数访问,所以我想在需要时包含规范的URL。
我认为最简单的解决方案是获取活动菜单项的URL,如果访问的URL与此不同,则将canonical-tag包含到header(模板的index.php)中。
我有基本的PHP知识,但我不知道如何获得活动菜单项的URL和访问的URL。有人能告诉我怎么得到这个信息吗?如果我有这个,那么我可以简单地比较它,如果它不同,添加标签。
请张贴一些如何获得这些信息的示例代码。
编辑:这里是一些基本信息(只与"主页"有关),但对我来说,这段代码不起作用。http://writenowdesign.com/joomla-tutorials/joomla-trips-and-tricks/add-canonical-url-link-to-joomla-home-page/
EDIT2:例如:
<?php
// Source: http://www.php.de/php-einsteiger/91123-erledigt-aktuelle-browser-url-ausgeben-funktion-zweckdienlich-2.html
function getUrl($arrAddParam = false, $xhtmlValid = false, $anchor = false, $dropCalledArgs = false) {
$url = !empty($_SERVER['HTTPS']) ? 'https://' : 'http://';
$url .= $_SERVER['SERVER_NAME'];
$url .= $_SERVER['SCRIPT_NAME'];
// merge input and user param arrays
$arrParam = is_array($arrAddParam) ? $arrAddParam : array();
if ($dropCalledArgs === false) $arrParam = array_merge($_GET,$arrParam);
if (!empty($arrParam)) {
$arg_separator = $xhtmlValid ? '&' : '&';
$url .= "?". http_build_query($arrParam, '', $arg_separator);
}
// anchor
if ($anchor !== false) {
$url .= '#'.$anchor;
}
return $url;
}
// Get the application object
$app = JFactory::getApplication();
// Get's the menu object
$menu = $app->getMenu();
// Current URL
$activeUrl = getUrl();
// SHOULD get the active menu target as string, but it's an object
// THIS is my question how to get the URL of the current active menu item
$menuUrl = $menu->getActive();
?>
编辑3:这里有可能得到这个url。但我需要SEO url,而不是"正常的一个"。
与此同时,我找到了一个解决方案。也许这对我的处境有所帮助:
<?php
// Get SEO Page URL
$pageUrl = JURI::current();
// Get URI Object
$uri = & JFactory::getURI();
// Get URI String
$pageUri = $uri->toString();
// Check if there is a difference
if ($pageUrl != $pageUri){
// Insert canonical tag when needed
echo '<link rel="canonical" href="'.$pageUrl.'" />';
}
?>
你的意思是这样,还是我错过了什么?
var loc = window.location.href;
$("ul.nav li a").each(function() {
if(this.href == loc) {
// If the current URL is the same as the active menu url
// Do something
}
});
这是官方书籍记录的获取当前url的方法
想把它们做为:getInstance ();
我将测试你的代码似乎是一个很好的开始。