Page 1 of 1

Adding option to link to named anchor in article

Posted: Thu Sep 24, 2009 8:22 pm
by djb_excal
Inside the components\com_content\views\article\tmpl\default.xml, I added

Code: Select all

            <param name="anchor" type="text" default="" label="Type Anchor Name (optional)" description="A named anchor in the article(optional)" />

That causes a textbox to show where you can enter the named anchor you want to link to when creating the menu item.

Inside modules\mod_mainmenu\helper.php, I changed

Code: Select all

		default :
                              $router = JSite::getRouter();
		    $tmp->url = $router->getMode() == JROUTER_MODE_SEF ? 'index.php?Itemid='.$tmp->id : $tmp->link.'&Itemid='.$tmp->id;
		break;
to

Code: Select all

			default :
				$router = JSite::getRouter();
				//$tmp->url = $router->getMode() == JROUTER_MODE_SEF ? 'index.php?Itemid='.$tmp->id : $tmp->link.'&Itemid='.$tmp->id;
				if($iParams->get('anchor') != ''){
				    $tmp->url = $router->getMode() == JROUTER_MODE_SEF ? 'index.php?Itemid='.$tmp->id : $tmp->link.'&Itemid='.$tmp->id.'#'.$iParams->get('anchor');
				}else{
				    $tmp->url = $router->getMode() == JROUTER_MODE_SEF ? 'index.php?Itemid='.$tmp->id : $tmp->link.'&Itemid='.$tmp->id;				
				}
				
		break;


I've also attached the files.

Re: Adding option to link to named anchor in article

Posted: Fri Nov 06, 2009 5:56 pm
by dhjoomla
djb_excal wrote:Inside the components\com_content\views\article\tmpl\default.xml, I added

Code: Select all

            <param name="anchor" type="text" default="" label="Type Anchor Name (optional)" description="A named anchor in the article(optional)" />

That causes a textbox to show where you can enter the named anchor you want to link to when creating the menu item.

Inside modules\mod_mainmenu\helper.php, I changed

Code: Select all

		default :
                              $router = JSite::getRouter();
		    $tmp->url = $router->getMode() == JROUTER_MODE_SEF ? 'index.php?Itemid='.$tmp->id : $tmp->link.'&Itemid='.$tmp->id;
		break;
to

Code: Select all

			default :
				$router = JSite::getRouter();
				//$tmp->url = $router->getMode() == JROUTER_MODE_SEF ? 'index.php?Itemid='.$tmp->id : $tmp->link.'&Itemid='.$tmp->id;
				if($iParams->get('anchor') != ''){
				    $tmp->url = $router->getMode() == JROUTER_MODE_SEF ? 'index.php?Itemid='.$tmp->id : $tmp->link.'&Itemid='.$tmp->id.'#'.$iParams->get('anchor');
				}else{
				    $tmp->url = $router->getMode() == JROUTER_MODE_SEF ? 'index.php?Itemid='.$tmp->id : $tmp->link.'&Itemid='.$tmp->id;				
				}
				
		break;


I've also attached the files.
Thanks alot for sharing!
But when I upload two files that I didn't see changing, I've insert the anchor in article. I am using Joomla 1.5.3. Do u show me how do you do? Step by step? Thanks!

Re: Adding option to link to named anchor in article

Posted: Sat Nov 07, 2009 4:57 pm
by rjscott2005
Hi-
I've also tried your two files and I don't see the text box for adding the anchor. Could there be something causing the issue such as a 3rd party template or extension?

I was however able to make the anchors work by creating a menu link that also included the Itemid with the anchor tag after it:

Code: Select all

index.php?option=com_content&view=article&id=31;Itemid=556;#p233
Works like a charm.

Re: Adding option to link to named anchor in article

Posted: Sun Nov 08, 2009 3:13 pm
by dhjoomla
rjscott2005 wrote:Hi-
I've also tried your two files and I don't see the text box for adding the anchor. Could there be something causing the issue such as a 3rd party template or extension?

I was however able to make the anchors work by creating a menu link that also included the Itemid with the anchor tag after it:

Code: Select all

index.php?option=com_content&view=article&id=31;Itemid=556;#p233
Works like a charm.
You can login into Administrator/Menu and choose the menu which you can use. In the right you can see that box.

Re: Adding option to link to named anchor in article

Posted: Sun Nov 08, 2009 3:30 pm
by rjscott2005
dhjoomla wrote:
rjscott2005 wrote:Hi-
I've also tried your two files and I don't see the text box for adding the anchor. Could there be something causing the issue such as a 3rd party template or extension?

I was however able to make the anchors work by creating a menu link that also included the Itemid with the anchor tag after it:

Code: Select all

index.php?option=com_content&view=article&id=31;Itemid=556;#p233
Works like a charm.
You can login into Administrator/Menu and choose the menu which you can use. In the right you can see that box.
I am not using the default admin template. Maybe that is why it isn't visible.

Re: Adding option to link to named anchor in article

Posted: Sun Nov 08, 2009 3:58 pm
by dhjoomla
Could you choose again default template then try again. I don't think that is cause.

Re: Adding option to link to named anchor in article

Posted: Sun Nov 08, 2009 4:12 pm
by rjscott2005
dhjoomla wrote:Could you choose again default template then try again. I don't think that is cause.
I will try that and post the results. It may be a while though because my little son is in need of my undivided attention at the moment. :eek:

Re: Adding option to link to named anchor in article

Posted: Mon Nov 09, 2009 6:33 am
by dhjoomla
Ok! You are good father and happiness! I will try yourself and post again.

Re: Adding option to link to named anchor in article

Posted: Mon Nov 09, 2009 12:26 pm
by rjscott2005
dhjoomla wrote:Ok! You are good father and happiness! I will try yourself and post again.
Thanks :laugh:
I've attached screen grabs of each template while using the modified default.xml and helper.php files.
menu_item_default_template.jpg
menu_item_APLite_template.jpg
Parameters Advanced for both templates states: There are no Parameters for this Menu Item

Re: Adding option to link to named anchor in article

Posted: Tue Nov 10, 2009 8:09 am
by dhjoomla
rjscott2005 wrote:
dhjoomla wrote:Ok! You are good father and happiness! I will try yourself and post again.
Thanks :laugh:
I've attached screen grabs of each template while using the modified default.xml and helper.php files.
menu_item_default_template.jpg
menu_item_APLite_template.jpg
Parameters Advanced for both templates states: There are no Parameters for this Menu Item
Oh, you only see if you choose type Internal link -> Article->Article Layout. I attached this my menu item. You can see it

Re: Adding option to link to named anchor in article

Posted: Tue Nov 10, 2009 10:49 am
by rjscott2005
dhjoomla wrote:
rjscott2005 wrote:
dhjoomla wrote:Ok! You are good father and happiness! I will try yourself and post again.
Thanks :laugh:
I've attached screen grabs of each template while using the modified default.xml and helper.php files.
menu_item_default_template.jpg
menu_item_APLite_template.jpg
Parameters Advanced for both templates states: There are no Parameters for this Menu Item
Oh, you only see if you choose type Internal link -> Article->Article Layout. I attached this my menu item. You can see it
Ah! I see it when I choose Internal Article. However after I enter the anchor, it still doesn't work. I've attached two screen grabs showing the anchor like

Code: Select all

#anchor
and like

Code: Select all

anchor
.
The outputted result is the same:

Code: Select all

http://www.corpsvets.org/index.php?option=com_content&view=article&id=31&Itemid=200129
Menu Item_ [Edit]_1257849541230.png
Menu Item_ [Edit]_1257849520808.png

Re: Adding option to link to named anchor in article

Posted: Thu Jan 14, 2010 1:17 pm
by decay1969
Hi,

Has anyone managed to get this hack working properly yet?

I also downloaded the above PHP files and have got the 'anchor' field to appear when creating a menu, I then enter the anchor ID in the newly created menu article link box, but it makes no difference?

Anyone have any ideas if it is ONLY possible to add anchor links in by changing the menu links to URL's instead of internal Article Links?

I have over 600 pages of Internal Article links that I want to add a simple ' #contenttop ' suffix too, but I don't want to change 600 Article Links to URLS!?

Anyone have any thoughts?

Thanks in advance.

D.

Re: Adding option to link to named anchor in article

Posted: Fri Jan 29, 2010 10:24 am
by h40534
I have managed to hack it successfully -

You need to edit 3 files ( two if you wish to put the anchor in each article )

1 -> administrator/components/com_menus/models/metadata/components.xml

After

<param name="menu_image" type="imagelist" directory="/images/stories" hide_default="1" default="" label="Menu Image" description="PARAMMENUIMAGE" />

add this line

Code: Select all

<param name ="anchor" type ="text" size="20" label="Named Anchor" description="named anchor , must include #"  default="#content"/>

2-> in modules/mod_mainmenu/helper.php , change

This

Code: Select all

    default :
                              $router = JSite::getRouter();
          $tmp->url = $router->getMode() == JROUTER_MODE_SEF ? 'index.php?Itemid='.$tmp->id : $tmp->link.'&Itemid='.$tmp->id;
      break;
to

This

Code: Select all

 default :
            $router = JSite::getRouter();
            if($iParams->get('anchor') != ''){
                $tmp->url = $router->getMode() == JROUTER_MODE_SEF ? 'index.php?Itemid='.$tmp->id : $tmp->link.'&Itemid='.$tmp->id.'#'.$iParams->get('anchor');
            }else{
                $tmp->url = $router->getMode() == JROUTER_MODE_SEF ? 'index.php?Itemid='.$tmp->id : $tmp->link.'&Itemid='.$tmp->id;            
            }
            
      break;



3->

In your active template , just before the

Code: Select all

 <jdoc:include type="component" />
place

Code: Select all

<a name ="content" />
and you are all done :)

PS : You can skip the last step if you wish to put the anchor in the article , or somewhere else in the template , just remember to change it from menu manager -> your menu -> parameters ->system

Shoot me a mail on [email protected] if you have any doubts.

Re: Adding option to link to named anchor in article

Posted: Mon Feb 01, 2010 11:41 am
by decay1969
Thanks for the clear explanation...i'll give it a try.

Dave.

Re: Adding option to link to named anchor in article

Posted: Mon Feb 01, 2010 11:34 pm
by jgohil
@h40534 - Your solution works, except there's no need to add the '#' - for whatever reason. When reviewing the generated URL, it's already added. If you do add it, it doubles up. Thanks for the solution though - it works great.

For those wondering why do it this way rather than simply using 'External Link' and adding the Alias there - is because you don't have the ability to publish Modules to the menu item if it's an External Link.

This solution preserves the 'Article' format (and the ability to publish specific modules to that page) while still providing a way to target specific content within that Article.

Thanks again!

Re: Adding option to link to named anchor in article

Posted: Tue Feb 02, 2010 8:38 am
by h40534
@jgohil - Thanks for mentioning that , i forgot to add it in the description :)

Re: Adding option to link to named anchor in article

Posted: Fri Feb 19, 2010 10:46 pm
by ncase
Thanks guys, any suggestions for making it work with the default Joomla! SEF Urls?

Re: Adding option to link to named anchor in article

Posted: Sat Feb 20, 2010 12:38 am
by ncase
decay1969 wrote:Hi,

Has anyone managed to get this hack working properly yet?

I also downloaded the above PHP files and have got the 'anchor' field to appear when creating a menu, I then enter the anchor ID in the newly created menu article link box, but it makes no difference?

Anyone have any ideas if it is ONLY possible to add anchor links in by changing the menu links to URL's instead of internal Article Links?

I have over 600 pages of Internal Article links that I want to add a simple ' #contenttop ' suffix too, but I don't want to change 600 Article Links to URLS!?

Anyone have any thoughts?

Thanks in advance.

D.
'h4053' code worked best for me. Be sure to make your menu links article/blog layout (not direct article). This works perfectly for non SEF urls as they #named-anchor is appended successfully to the url - However with SEF Urls enabled in the global configuration Joomla treats the new menu item alias as a sub section of the root page (basically it puts a '/' instead of a '#').

since special characters can't be used for unique aliases, simply adding the # to that won't work either. Since SEF URLs are more important I'm having to look for an alternate way to push directly to named anchors from menu items

Re: Adding option to link to named anchor in article

Posted: Thu Jul 15, 2010 12:43 am
by normdouglas
Just found this topic, and thought I would share how I just solved this problem.

The issue was that my client wanted to be able to have me fire off external links that landed on the blog layout page, but jumped down to the specific blog item (hence an anchor link). Trouble is that Joomla doesn't by default offer an "Anchor" tag option.

So, by using Template Overrides, I added the follow line to the blog_item.php file right below the area that produces the article title.

Code: Select all

<a name="<?php echo $this->escape($this->item->alias); ?>" id="<?php echo $this->escape($this->item->alias); ?>"></a>
Here is an example of this working.
http://www.villawoodproperties.com.au/v ... tlandsales
Which is an anchor link to a specific article contained within a category blog layout.

This shows yet again the power of template overrides, as this now puts an anchor link named the same as the article alias right below every article title across all blogs on your site.

You will need to learn more about Template overrides before implementing this technique.

Good luck

Re: Adding option to link to named anchor in article

Posted: Fri Sep 24, 2010 12:32 am
by Sushikidd
Norm, could you be more specific (just pretend im a noob.. ;) )about where to paste that code pls?

thnx, youre a great help!



Sid

Re: Adding option to link to named anchor in article

Posted: Fri Sep 24, 2010 9:53 am
by ooffick
  1. create a folder in your template folder called "html" (if it doesn't exists)
  2. create a folder in that html folder called "com_content" (if it doesn't exists)
  3. create a folder in that com_content folder called "category" (if it doesn't exists)
  4. copy the file .../components/com_content/views/category/tmpl/blog_item.php in this .../templates/[your-template]/html/com_content/category/ folder
  5. open that file
  6. find the following lines:

    Code: Select all

    <a href="<?php echo $this->item->readmore_link; ?>" class="contentpagetitle<?php echo $this->escape($this->item->params->get( 'pageclass_sfx' )); ?>">
  7. and replace it with the following:

    Code: Select all

    <a name="<?php echo $this->escape($this->item->alias); ?>" id="<?php echo $this->escape($this->item->alias); ?>"></a>
    <a href="<?php echo $this->item->readmore_link; ?>" class="contentpagetitle<?php echo $this->escape($this->item->params->get( 'pageclass_sfx' )); ?>">
  8. save the file
You might need to use a different view, e.g. the section view, or the frontpage view.

Olaf

Re: Adding option to link to named anchor in article

Posted: Sun Sep 26, 2010 7:56 am
by Jay44
If all you need is a mune item pointing to an anchor within an article, then rjscott2005's simple way works just fine.

Note you must chose External Link as the menu type.

You can see this in his attached screenshots.

Answered my question straight up! Thanks rjscott2005 (your little son will be 5 years older as I read this!)

J

Re: Adding option to link to named anchor in article

Posted: Sun Sep 26, 2010 2:53 pm
by Sushikidd
ooffick wrote:
  1. create a folder in your template folder called "html" (if it doesn't exists)
  2. create a folder in that html folder called "com_content" (if it doesn't exists)
  3. create a folder in that com_content folder called "category" (if it doesn't exists)
  4. copy the file .../components/com_content/views/category/tmpl/blog_item.php in this .../templates/[your-template]/html/com_content/category/ folder
  5. open that file
  6. find the following lines:

    Code: Select all

    <a href="<?php echo $this->item->readmore_link; ?>" class="contentpagetitle<?php echo $this->escape($this->item->params->get( 'pageclass_sfx' )); ?>">
  7. and replace it with the following:

    Code: Select all

    <a name="<?php echo $this->escape($this->item->alias); ?>" id="<?php echo $this->escape($this->item->alias); ?>"></a>
    <a href="<?php echo $this->item->readmore_link; ?>" class="contentpagetitle<?php echo $this->escape($this->item->params->get( 'pageclass_sfx' )); ?>">
  8. save the file
You might need to use a different view, e.g. the section view, or the frontpage view.

Olaf


Fantastic! thanks! :)

Re: Adding option to link to named anchor in article

Posted: Tue Oct 19, 2010 12:14 am
by potski205
Thank you for this post, it worked great for me.

I wonder if it is possible to add this anchor functionality for the contact component? I am fairly new to this so any help would be appreciated on how and where to add code to allow linking to an anchor (e.g. top of article) on a contact page. I mean, where to add the actual anchor code as you can't edit HTML like in a normal article.

Thank you!

Re: Adding option to link to named anchor in article

Posted: Tue Dec 14, 2010 12:34 am
by zbabyface
ok, so this is going to kill you guys.....this is only for someone that wants a quick Menu Item External link to an anchor in an article and is using Joomla SEF settings.

Step 1: Create an html file and name it whatever you'd like
- ex: http://yoursite.com/anchor.html
- add the following to the html file:

<html>
<head>
<title>History of On-Cor</title>


<meta http-equiv="refresh" content="0;url=http://www.yoursite.com/about.html#anchor">
</head>
<body>
</body>
</html>

Step 2:
Create an external link to an article:
ex: http://yoursite.com/anchor.html

Step 3: publish new menu item!

Re: Adding option to link to named anchor in article

Posted: Tue Dec 14, 2010 7:17 am
by richardphilips
Although I took me some time to understand what you all are trying to achieve here, but @ooffick and "template overriding" made the picture crystal clear.

Instead of hacking any files, template overriding is always better option. Never never try to hack any joomla files unless its absolutely necessary.

This way your core joomla files are intact and you can upgrade to latest version of joomla without worrying about your changes getting over written.

Re: Adding option to link to named anchor in article

Posted: Sat Jan 22, 2011 4:43 am
by Gothic Dev
All these solutions seem quite complex.
Why not simply add &#anchor at the end of the URL?

Example:

Code: Select all

index.php?option=com_content&view=article&id=47&Itemid=57&#anchor
where
  • index.php is the Home URL for Joomla
  • option=com_content&view=article&id=47 is the place you are going to
  • Itemid=57 is the place you are coming from
  • #anchor is the name of your anchor
This approach will preserve breadcrumbs and rewritten localized URLs, whereas using an external link would not.
Tested this in Joomla 1.5.22

X