简体   繁体   English

Joomla:如何获取关联菜单项的URL(多语言/从当前页面)

[英]Joomla: How to get the URL of an associated menu item (Multilingual / from current page)

i want to manually add a hreflang-attribute to the HTML-Head of a page. 我想手动将hreflang属性添加到页面的HTML标头中。

I'm using Joomla 3.4.8 + SEBLOD component for a multilingual site. 我正在将Joomla 3.4.8 + SEBLOD组件用于多语言站点。 If I associate menu items of different languages, usually a "link-tag" and a "hreflang-attribute" are added to the head to make the relationship between the menu items visible for search engines. 如果我关联了不同语言的菜单项,通常会将“链接标签”和“ hreflang属性”添加到标题中,以使菜单项之间的关系对于搜索引擎可见。

For some reason, this tag is not added, if I'm in a list-and-search-view of the Seblod component. 由于某种原因,如果我在Seblod组件的列表和搜索视图中,则不会添加此标记。 That's why I manually want to write the missing tags to the head. 这就是为什么我要手动将丢失的标签写到头部的原因。

I know how to add meta information to the head and how to get the ID of the current menu item, but I can't figure out how to get the ID (or SEO-friendly URL) of the associated menu items of my current page. 我知道如何向头部添加元信息以及如何获取当前菜单项的ID, 但是我不知道如何获取当前页面相关菜单项的ID(或SEO友好URL)

Any ideas? 有任何想法吗?

I hope I described myself well... Thanks in advance! 我希望我能很好地描述自己...预先感谢!

Example: The menu item of the current page is in german language. 示例:当前页面的菜单项是德语。 It is associated / connected with a menu item in english language (which is showing the english version of the current page). 它与英语菜单项相关联/相连(该菜单项显示当前页面的英语版本)。 What I need is the SEF-URL of the english menu item. 我需要的是英语菜单项的SEF-URL。

The easiest solution would be to query the active menu so you get all of the variables for the menu. 最简单的解决方案是查询活动菜单,以便获得菜单的所有变量。 The code below should return the current active route for the menu: 下面的代码应返回菜单的当前活动路线:

$app = JFactory::getApplication();
$menu   = $app->getMenu()->getActive();

$sefUrl = $menu->route;

echo '<pre>';
print_r($menu);
echo '</pre>';

echo $sefUrl;

//HERE IS THE PHP TO SET A META DYNAMICALLY IF YOU NEED IT AS WELL
$doc =& JFactory::getDocument();
$doc->setMetaData( 'tag-name', 'tag-content', true );

If the route that is being returned is not the correct route that matches your page you can check to see if the link attribute includes index.php using strpos and then concatenate that and the menu id to run through JRoute for an SEF url as well like so: 如果返回的路由不是与您的页面匹配的正确路由,则可以使用strpos检查链接属性是否包含index.php,然后将其和菜单ID串联起来,以通过JRoute运行SEF网址,例如所以:

if(strpos($menu-Link,'index.php?') !== false){
    $sefUrl = JRoute::_($menu->link.'&Itemid='.$menu->id);
}

Hope this works for you. 希望这对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM