2013-01-03 53 views
0

由於Joomla 2.5沒有提供規範URL的工作插件(除SEF404外),我會嘗試「模仿」我的模板並在需要時包含規範URL。Get Active Menu-URL

因爲每個菜單項都可以通過不同的URL和GET參數訪問,所以我希望在需要時包含規範URL。

我認爲最簡單的解決方案是獲取活動菜單項的URL,如果訪問的url不同於此,則將標準標籤包含到模板的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 ? '&amp;' : '&'; 
     $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:Here是可能得到這個網址。但我需要SEO網址,而不是「普通網址」。

回答

1

在此期間,我找到了解決方案。也許這可以幫助別人在我的情況:

<?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.'" />'; 
} 

?> 
0

你的意思是這樣嗎?還是我錯過了什麼?

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 
    } 
}); 
+0

這是通過JS的當前網址 - 是的:) - 我會重新編輯主要問題,以清除它更詳細一點。 – Andy

+0

看看「編輯2」:) – Andy

0

這是記錄在案的方式來獲得當前的URL

JURI ::的getInstance()官方書籍;

我會測試你的代碼似乎是一個很好的開始。

+0

Andy - 你的解決方案不適合我,總是這兩個變量是相同的,因此沒有規範插入。我認爲我們需要獲得php值和joomla值。 試試這個http://www.shikle.com/metagenerator.htm – landed