Adding option to link to named anchor in article

Your code modifications and patches you want to share with others.
djb_excal
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Fri Sep 18, 2009 2:35 pm

Adding option to link to named anchor in article

Postby djb_excal » Thu Sep 24, 2009 8:22 pm

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.
You do not have the required permissions to view the files attached to this post.

dhjoomla
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Fri Nov 06, 2009 3:54 pm

Re: Adding option to link to named anchor in article

Postby dhjoomla » Fri Nov 06, 2009 5:56 pm

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!

User avatar
rjscott2005
Joomla! Intern
Joomla! Intern
Posts: 93
Joined: Mon Aug 22, 2005 6:41 pm
Location: Dallas, Georgia U.S.
Contact:

Re: Adding option to link to named anchor in article

Postby rjscott2005 » Sat Nov 07, 2009 4:57 pm

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.
RJ
Atlanta, Georgia, US

dhjoomla
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Fri Nov 06, 2009 3:54 pm

Re: Adding option to link to named anchor in article

Postby dhjoomla » Sun Nov 08, 2009 3:13 pm

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.

User avatar
rjscott2005
Joomla! Intern
Joomla! Intern
Posts: 93
Joined: Mon Aug 22, 2005 6:41 pm
Location: Dallas, Georgia U.S.
Contact:

Re: Adding option to link to named anchor in article

Postby rjscott2005 » Sun Nov 08, 2009 3:30 pm

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.
RJ
Atlanta, Georgia, US

dhjoomla
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Fri Nov 06, 2009 3:54 pm

Re: Adding option to link to named anchor in article

Postby dhjoomla » Sun Nov 08, 2009 3:58 pm

Could you choose again default template then try again. I don't think that is cause.

User avatar
rjscott2005
Joomla! Intern
Joomla! Intern
Posts: 93
Joined: Mon Aug 22, 2005 6:41 pm
Location: Dallas, Georgia U.S.
Contact:

Re: Adding option to link to named anchor in article

Postby rjscott2005 » Sun Nov 08, 2009 4:12 pm

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:
RJ
Atlanta, Georgia, US

dhjoomla
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Fri Nov 06, 2009 3:54 pm

Re: Adding option to link to named anchor in article

Postby dhjoomla » Mon Nov 09, 2009 6:33 am

Ok! You are good father and happiness! I will try yourself and post again.

User avatar
rjscott2005
Joomla! Intern
Joomla! Intern
Posts: 93
Joined: Mon Aug 22, 2005 6:41 pm
Location: Dallas, Georgia U.S.
Contact:

Re: Adding option to link to named anchor in article

Postby rjscott2005 » Mon Nov 09, 2009 12:26 pm

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
You do not have the required permissions to view the files attached to this post.
RJ
Atlanta, Georgia, US

dhjoomla
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Fri Nov 06, 2009 3:54 pm

Re: Adding option to link to named anchor in article

Postby dhjoomla » Tue Nov 10, 2009 8:09 am

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
You do not have the required permissions to view the files attached to this post.

User avatar
rjscott2005
Joomla! Intern
Joomla! Intern
Posts: 93
Joined: Mon Aug 22, 2005 6:41 pm
Location: Dallas, Georgia U.S.
Contact:

Re: Adding option to link to named anchor in article

Postby rjscott2005 » Tue Nov 10, 2009 10:49 am

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
You do not have the required permissions to view the files attached to this post.
RJ
Atlanta, Georgia, US

decay1969
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Thu Jan 14, 2010 12:18 pm

Re: Adding option to link to named anchor in article

Postby decay1969 » Thu Jan 14, 2010 1:17 pm

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.

h40534
Joomla! Apprentice
Joomla! Apprentice
Posts: 11
Joined: Sun Mar 09, 2008 7:19 am

Re: Adding option to link to named anchor in article

Postby h40534 » Fri Jan 29, 2010 10:24 am

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.
Offering professional PSd to Joomla Template Conversion services , Custom Joomla Templates , Joomla trobuleshooting , html to joomla ,Custom components- I love joomla !!!
http://www.joompros.com

decay1969
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Thu Jan 14, 2010 12:18 pm

Re: Adding option to link to named anchor in article

Postby decay1969 » Mon Feb 01, 2010 11:41 am

Thanks for the clear explanation...i'll give it a try.

Dave.

User avatar
jgohil
Joomla! Apprentice
Joomla! Apprentice
Posts: 23
Joined: Sat Jun 09, 2007 11:01 pm
Location: St. Louis, MO - USA
Contact:

Re: Adding option to link to named anchor in article

Postby jgohil » Mon Feb 01, 2010 11:34 pm

@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!

h40534
Joomla! Apprentice
Joomla! Apprentice
Posts: 11
Joined: Sun Mar 09, 2008 7:19 am

Re: Adding option to link to named anchor in article

Postby h40534 » Tue Feb 02, 2010 8:38 am

@jgohil - Thanks for mentioning that , i forgot to add it in the description :)
Offering professional PSd to Joomla Template Conversion services , Custom Joomla Templates , Joomla trobuleshooting , html to joomla ,Custom components- I love joomla !!!
http://www.joompros.com

ncase
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Fri Feb 19, 2010 6:20 pm

Re: Adding option to link to named anchor in article

Postby ncase » Fri Feb 19, 2010 10:46 pm

Thanks guys, any suggestions for making it work with the default Joomla! SEF Urls?

ncase
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Fri Feb 19, 2010 6:20 pm

Re: Adding option to link to named anchor in article

Postby ncase » Sat Feb 20, 2010 12:38 am

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

User avatar
normdouglas
Joomla! Hero
Joomla! Hero
Posts: 2653
Joined: Fri Jan 20, 2006 11:37 am
Location: Geelong Australia

Re: Adding option to link to named anchor in article

Postby normdouglas » Thu Jul 15, 2010 12:43 am

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
Step1. Read the instructions. Step2. Refer to step 1. Step 3. Backup

Sushikidd
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Fri Sep 24, 2010 12:24 am

Re: Adding option to link to named anchor in article

Postby Sushikidd » Fri Sep 24, 2010 12:32 am

Norm, could you be more specific (just pretend im a noob.. ;) )about where to paste that code pls?

thnx, youre a great help!



Sid

User avatar
ooffick
Joomla! Master
Joomla! Master
Posts: 11075
Joined: Thu Jul 17, 2008 3:10 pm
Location: Ireland
Contact:

Re: Adding option to link to named anchor in article

Postby ooffick » Fri Sep 24, 2010 9:53 am

  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
Olaf Offick - Global Moderator
http://learnskills.org

User avatar
Jay44
Joomla! Ace
Joomla! Ace
Posts: 1118
Joined: Fri Oct 19, 2007 10:34 pm
Location: Australia

Re: Adding option to link to named anchor in article

Postby Jay44 » Sun Sep 26, 2010 7:56 am

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
"Things not being necessary is what makes life interesting" -- Stephen Fry
This is necessary - http://docs.joomla.org/

Sushikidd
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Fri Sep 24, 2010 12:24 am

Re: Adding option to link to named anchor in article

Postby Sushikidd » Sun Sep 26, 2010 2:53 pm

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! :)

potski205
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Tue Oct 19, 2010 12:08 am

Re: Adding option to link to named anchor in article

Postby potski205 » Tue Oct 19, 2010 12:14 am

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!

zbabyface
Joomla! Apprentice
Joomla! Apprentice
Posts: 30
Joined: Fri Sep 19, 2008 4:16 pm

Re: Adding option to link to named anchor in article

Postby zbabyface » Tue Dec 14, 2010 12:34 am

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!

User avatar
richardphilips
Joomla! Intern
Joomla! Intern
Posts: 52
Joined: Thu Sep 09, 2010 6:20 pm
Location: LOS ANGELES, CA 90015 UNITED STATES
Contact:

Re: Adding option to link to named anchor in article

Postby richardphilips » Tue Dec 14, 2010 7:17 am

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.
Joomla Rocks :)
Thanks and Regards, Richard Philips
http://www.htmltojoomla.com
HTML to Joomla Conversion | Convert Static HTML website to Joomla Website

Gothic Dev
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Sat Jan 22, 2011 4:26 am

Re: Adding option to link to named anchor in article

Postby Gothic Dev » Sat Jan 22, 2011 4:43 am

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


Return to “Core Hacks and Patches”

Who is online

Users browsing this forum: No registered users and 0 guests