How to add the tag 'title' in menu links v1.5.20

Discuss Search Engine Optimization in relation to Joomla!. This forum will also have discussions on SEF/SEO Joomla! extensions.

Moderator: General Support Moderators

Locked
Aneurysm
Joomla! Apprentice
Joomla! Apprentice
Posts: 32
Joined: Wed Apr 28, 2010 3:30 pm
Location: Italy

How to add the tag 'title' in menu links v1.5.20

Post by Aneurysm » Tue Sep 07, 2010 1:47 pm

Hi everybody,
How can I add the tag 'title' into the menu links? - Joomla version 1.5.20.

Like:
<a title="something" href="............">

maybe by some extensions or by modifying a file?
Thanx in advance.

Alex Dobrin
Joomla! Hero
Joomla! Hero
Posts: 2487
Joined: Wed Jun 07, 2006 9:10 am
Location: Brasov - Romania
Contact:

Re: How to add the tag 'title' in menu links v1.5.20

Post by Alex Dobrin » Tue Sep 07, 2010 2:07 pm

You can modify the output of the menu module using output overrides:
http://developer.joomla.org/tutorials/1 ... oomla.html
My latest project - http://www.extraglaze.co.uk/

Aneurysm
Joomla! Apprentice
Joomla! Apprentice
Posts: 32
Joined: Wed Apr 28, 2010 3:30 pm
Location: Italy

Re: How to add the tag 'title' in menu links v1.5.20

Post by Aneurysm » Tue Sep 07, 2010 3:04 pm

Ok, I found out how to do it, basically it's enough to add just the title="'.$item->name.'" into the a tag, correct me if I'm wrong.

Open the file modules\mod_mainmenu\helper.php and change these lines:

Line 363:

Code: Select all

$data = '<a href="'.$tmp->url.'">'.$image.$tmp->name.'</a>';
Change to:

Code: Select all

$data = '<a title="'.$item->name.'" href="'.$tmp->url.'">'.$image.$tmp->name.'</a>';
Line 367:

Code: Select all

$data = '<a title="'.$item->name.'" href="'.$tmp->url.'" target="_blank">'.$image.$tmp->name.'</a>';
Change to:

Code: Select all

$data = '<a title="'.$item->name.'" href="'.$tmp->url.'" target="_blank">'.$image.$tmp->name.'</a>';
Line 375:

Code: Select all

$data = '<a title="'.$item->name.'" href="'.$link.'" onclick="window.open(this.href,\'targetWindow\',\''.$attribs.'\');return false;">'.$image.$tmp->name.'</a>';
Change to:

Code: Select all

$data = '<a title="'.$item->name.'" href="'.$link.'" onclick="window.open(this.href,\'targetWindow\',\''.$attribs.'\');return false;">'.$image.$tmp->name.'</a>';

Alex Dobrin
Joomla! Hero
Joomla! Hero
Posts: 2487
Joined: Wed Jun 07, 2006 9:10 am
Location: Brasov - Romania
Contact:

Re: How to add the tag 'title' in menu links v1.5.20

Post by Alex Dobrin » Tue Sep 07, 2010 3:23 pm

It's not a good idea to modify the core code. That's why I suggest output overrides.

You can copy the file :

Code: Select all

modules\mod_mainmenu\tmpl\default.php
into the folder :

Code: Select all

templates\your_template\html\mod_mainmenu\
and modify it to add the title attribute to each link.

Above the line :

Code: Select all

	$node->removeAttribute('rel');
add the line :

Code: Select all

	$node->addAttribute('title', $node->name());
My latest project - http://www.extraglaze.co.uk/

Aneurysm
Joomla! Apprentice
Joomla! Apprentice
Posts: 32
Joined: Wed Apr 28, 2010 3:30 pm
Location: Italy

Re: How to add the tag 'title' in menu links v1.5.20

Post by Aneurysm » Tue Sep 07, 2010 6:59 pm

It doesn't function, I used Artisteer to make my personal template and the modifications that you suggest make my styled menù like a normal menu, plus I get the text "span" as menu title tag.

Alex Dobrin
Joomla! Hero
Joomla! Hero
Posts: 2487
Joined: Wed Jun 07, 2006 9:10 am
Location: Brasov - Romania
Contact:

Re: How to add the tag 'title' in menu links v1.5.20

Post by Alex Dobrin » Wed Sep 08, 2010 3:46 am

Then I'm guessing you already had that file in your template and you need to make the modifications directly into the one generated by Artisteer. I've tested the code before posting it and it is working in a template that doesn't already have the output overrides for the menu.
My latest project - http://www.extraglaze.co.uk/

herbun
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Fri Sep 10, 2010 4:23 pm

Re: How to add the tag 'title' in menu links v1.5.20

Post by herbun » Fri Sep 10, 2010 4:29 pm

Hi Alex,

thanks for helping those of us out who want a title tag. I tried your change in the default.php, the title tag shows up, however every menu title is named "span" (title="span"). It seems like $node->name() only gets the latest tag that was stored, in that case a span tag, or li, ul, etc..., depending on where the $node->name() is being placed in default.php (however I followed stricktly your instructions).
I could not find out how to access the menu title, the menu article title, or the article alias from within the default.php - maybe you have an idea?

thanks alot

EDIT:
instead of $node->name() use $node->data(). this will display the menu name and display it in the title tag. however - anyone got an idea how to access the article alias or article title instead?

Aneurysm
Joomla! Apprentice
Joomla! Apprentice
Posts: 32
Joined: Wed Apr 28, 2010 3:30 pm
Location: Italy

Re: How to add the tag 'title' in menu links v1.5.20

Post by Aneurysm » Sat Sep 11, 2010 6:15 pm

For now I'll keep the modifications as I specified here
It seems that in Joomla 1.6 there will be the integrated option to set a title for a menu item, is it true?
Is there some extension that do this also for 1.5.x ?
Anyway thanx for your help.

Alex Dobrin
Joomla! Hero
Joomla! Hero
Posts: 2487
Joined: Wed Jun 07, 2006 9:10 am
Location: Brasov - Romania
Contact:

Re: How to add the tag 'title' in menu links v1.5.20

Post by Alex Dobrin » Mon Sep 13, 2010 7:11 am

Sorry for the delay.

I've taken another look into this and found that the layout is used only when the "Menu Style" is set to "List" and instead of "$node->addAttribute('title', $node->name());" ( which for some reasons was working on the original page I've tested, but it was only a temporary instance of Jomla and have deleted it since then ) you can use this code:

Code: Select all

	if ($node->name() == 'a') {
		$children = $node->children();
		$node->addAttribute('title', $children[0]->_data);
	}
My latest project - http://www.extraglaze.co.uk/

Aneurysm
Joomla! Apprentice
Joomla! Apprentice
Posts: 32
Joined: Wed Apr 28, 2010 3:30 pm
Location: Italy

Re: How to add the tag 'title' in menu links v1.5.20

Post by Aneurysm » Mon Sep 13, 2010 9:32 am

Alex Dobrin wrote:Sorry for the delay.

I've taken another look into this and found that the layout is used only when the "Menu Style" is set to "List" and instead of "$node->addAttribute('title', $node->name());" ( which for some reasons was working on the original page I've tested, but it was only a temporary instance of Jomla and have deleted it since then ) you can use this code:

Code: Select all

	if ($node->name() == 'a') {
		$children = $node->children();
		$node->addAttribute('title', $children[0]->_data);
	}
Thanx Alex, here's the file \templates\my_template\html\mod_mainmenu\default.php
Where do I have do add the above code?

Code: Select all

<?php
defined('_JEXEC') or die('Restricted access'); // no direct access

ob_start();
require_once realpath(dirname(__FILE__) . str_replace('/', DIRECTORY_SEPARATOR, '/../../../../modules/mod_mainmenu/tmpl/default.php'));
ob_clean();

if (!defined('modMainMenuArtxExtensions'))
{

    function modMainMenuArtXMLCallback(&$node, $args)
    {
        $options = $GLOBALS['modMainMenuArtXMLCallbackOptions'];
        if ($node->name() == 'li') {
            if (!$options['show_submenus'] && $node->level() == 1) {
                if ($ul = $node->getElementByPath('ul'))
                    $node->removeChild($ul);
            }
            $liChildren = & $node->_children;
            if (count($liChildren) > 0) {
                // element                 ( $img?,                                          $span ( $text     )  )      )
                // <a href="...">          <img src="..." (align="left|right")? alt="..." />?<span><![CDATA[...]]></span></a>
                // <span class="separator"><img src="..." (align="left|right")? alt="..." />?<span><![CDATA[...]]></span></span>
                $element = & $liChildren[0];
                if ($element->_children[0]->name() == 'img') {
                    $img = & $element->_children[0];
                    $span = & $element->_children[1];
                    $element->removeChild($img);
                } else {
                    $img = null;
                    $span = & $element->_children[0];
                }
                $element->removeChild($span);
                // convert separator to anchor
                if ($element->name() == 'span' && $element->attributes('class') == 'separator') {
                    $element->_name = 'a';
                    $element->addAttribute('href', '#');
                    $element->addAttribute('onclick', 'return false;');
                }
                // add extra spans for top level items, required for design
                if ($element->level() == ($options['start'] + 1) * 2) {
                    $lspan = & $element->addChild('span', array('class' => 'l'));
                    $lspan->setData(' ');
                    $rspan = & $element->addChild('span', array('class' => 'r'));
                    $rspan->setData(' ');
                    $tspan = & $element->addChild('span', array('class' => 't'));
                    $container = & $tspan;
                } else {
                    $container = & $element;
                }
                // to prevent span from collapsing
                $text = strlen($span->data()) == 0 ? ' ' : $span->data();
                // container is always <a ...>...</a> here
                if ($img != null) {
                    $align = $img->attributes('align');
                    if ($align == 'left' || $align == '') {
                        $container->addChild('img', array('class' => 'art-metadata-icon',
                            'src' => $img->attributes('src'),
                            'style' => 'margin-right: 5px; vertical-align: middle;',
                            'alt' => $img->attributes('alt')));
                        $extraArtxSpan = & $container->addChild('artx-extra-span');
                        $extraArtxSpan->setData($text);
                    } else if ($align == 'right') {
                        $extraArtxSpan = & $container->addChild('artx-extra-span');
                        $extraArtxSpan->setData($text);
                        $container->addChild('img', array('class' => 'art-metadata-icon',
                            'src' => $img->attributes('src'),
                            'style' => 'margin-left: 5px; vertical-align: middle;',
                            'alt' => $img->attributes('alt')));
                    }
                } else {
                    $container->setData($text);
                }
            }

            modMainMenuXMLCallback($node, $args);

            // this should be after modMainMenuXMLCallback, because the callback sets class='active' and id='current'
            if ($options['vmenu'] !== null && !$options['vmenu']['simple']) {
                $class = $node->attributes('class');
                $isActive = $class && false !== strpos(' ' . $class, ' active');
                if ($node->attributes('id') != 'current' && !$isActive) {
                    if ($ul = $node->getElementByPath('ul'))
                        $node->removeChild($ul);
                }
            }

            $class = $node->attributes('class');
            if ($class && false !== strpos(' ' . $class, ' active')) {
                $element->addAttribute('class', 'active');
            }
        } else {
            modMainMenuXMLCallback($node, $args);
        }

    }

    function artxMenuDecorator($content)
    {
        $result = '';
        ob_start();
?>
<div class="art-nav">
    <div class="l"></div>
    <div class="r"></div>
<?php
        $result .= ob_get_clean() . $content;
        ob_start();
?>
</div>
<?php
        $result .= ob_get_clean();
        return $result;
    }


    define('modMainMenuArtxExtensions', true);
}

if (isset($attribs['name']) && $attribs['name'] == 'user3') {
    $GLOBALS['modMainMenuArtXMLCallbackOptions'] = array(
        'show_submenus' => $GLOBALS['artx_settings']['menu']['show_submenus'] && 1 == $params->get('showAllChildren'),
        'vmenu' => null,
        'start' => $params->get('startLevel')
    );
    $xml = modMainMenuHelper::getXML($params->get('menutype'), $params, 'modMainMenuArtXMLCallback');
    if ($xml) {
        $xml->addAttribute('class', 'art-menu');
        if ($tagId = $params->get('tag_id')) {
            $xml->addAttribute('id', $tagId);
        }
        $result = JFilterOutput::ampReplace($xml->toString((bool)$params->get('show_whitespace')));
        $result = str_replace(array('<ul/>', '<ul />', '<artx-extra-span>', '</artx-extra-span>'), '', $result);
        echo artxMenuDecorator($result);
    }
    unset($GLOBALS['tmp_menu_show_submenus']);
} else if ($params->get('moduleclass_sfx') == 'art-vmenu') {
    $GLOBALS['modMainMenuArtXMLCallbackOptions'] = array(
        'show_submenus' => $GLOBALS['artx_settings']['vmenu']['show_submenus'] && 1 == $params->get('showAllChildren'),
        'vmenu' => array('simple' => $GLOBALS['artx_settings']['vmenu']['simple']),
        'start' => $params->get('startLevel')
    );
    $xml = modMainMenuHelper::getXML($params->get('menutype'), $params, 'modMainMenuArtXMLCallback');
    if ($xml) {
        $xml->addAttribute('class', 'art-vmenu');
        if ($tagId = $params->get('tag_id')) {
            $xml->addAttribute('id', $tagId);
        }
        $result = JFilterOutput::ampReplace($xml->toString((bool)$params->get('show_whitespace')));
        $result = str_replace(array('<ul/>', '<ul />', '<artx-extra-span>', '</artx-extra-span>'), '', $result);
        echo $result;
    }
    unset($GLOBALS['tmp_menu_show_submenus']);
} else {
    modMainMenuHelper::render($params, 'modMainMenuXMLCallback');
}

Alex Dobrin
Joomla! Hero
Joomla! Hero
Posts: 2487
Joined: Wed Jun 07, 2006 9:10 am
Location: Brasov - Romania
Contact:

Re: How to add the tag 'title' in menu links v1.5.20

Post by Alex Dobrin » Mon Sep 13, 2010 12:02 pm

You can try adding that above the line "$liChildren = & $node->_children;".
My latest project - http://www.extraglaze.co.uk/

herbun
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Fri Sep 10, 2010 4:23 pm

Re: How to add the tag 'title' in menu links v1.5.20

Post by herbun » Mon Sep 13, 2010 3:32 pm

Unfortunately Aneurysm pasted the default.php from his custom template, where does one have to place the code in the standard default.php?

Thanks!

Alex Dobrin
Joomla! Hero
Joomla! Hero
Posts: 2487
Joined: Wed Jun 07, 2006 9:10 am
Location: Brasov - Romania
Contact:

Re: How to add the tag 'title' in menu links v1.5.20

Post by Alex Dobrin » Mon Sep 13, 2010 4:18 pm

herbun wrote:Unfortunately Aneurysm pasted the default.php from his custom template, where does one have to place the code in the standard default.php?
Just above this line ( do not remove it ):

Code: Select all

$node->removeAttribute('rel');
My latest project - http://www.extraglaze.co.uk/

herbun
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Fri Sep 10, 2010 4:23 pm

Re: How to add the tag 'title' in menu links v1.5.20

Post by herbun » Mon Sep 13, 2010 4:29 pm

Hi Alex,

I tried it already but it works only for menu items that are not primary level items. The solution I posted above works(but only thanks to your previous work). However, do you have any idea how to place content into the now working title tag like the title one can define in the menu item properties or the title of the article that the menu item links to, instead of only the link text of that menu item? One could specify different title tags this way, but I do not know how to access this data to place it there...

Thanks

Aneurysm
Joomla! Apprentice
Joomla! Apprentice
Posts: 32
Joined: Wed Apr 28, 2010 3:30 pm
Location: Italy

Re: How to add the tag 'title' in menu links v1.5.20

Post by Aneurysm » Mon Sep 13, 2010 4:31 pm

Alex Dobrin wrote:You can try adding that above the line "$liChildren = & $node->_children;".
Thanx, I'll give it a try

Aneurysm
Joomla! Apprentice
Joomla! Apprentice
Posts: 32
Joined: Wed Apr 28, 2010 3:30 pm
Location: Italy

Re: How to add the tag 'title' in menu links v1.5.20

Post by Aneurysm » Mon Sep 13, 2010 4:33 pm

herbun wrote:... One could specify different title tags this way, but I do not know how to access this data to place it there...

Thanks
It should be done with an extension/plugin, and I think there's nothing like that.

Alex Dobrin
Joomla! Hero
Joomla! Hero
Posts: 2487
Joined: Wed Jun 07, 2006 9:10 am
Location: Brasov - Romania
Contact:

Re: How to add the tag 'title' in menu links v1.5.20

Post by Alex Dobrin » Mon Sep 13, 2010 6:02 pm

herbun wrote: However, do you have any idea how to place content into the now working title tag like the title one can define in the menu item properties or the title of the article that the menu item links to, instead of only the link text of that menu item? One could specify different title tags this way, but I do not know how to access this data to place it there...
That depends on what information you want to have there.
If you have a link to an article and you want it's title you can get the ID of the article from the link's URL and get the title of the article directly from the database.
My latest project - http://www.extraglaze.co.uk/


Locked

Return to “Search Engine Optimization (Joomla! SEO) in Joomla! 1.5”