scriptygoddess

10 Sep, 2009

Anchor Links in WordPress Posts – another shortcode solution

Posted by: Jennifer In: WordPress|WordPress Hacks

I was recently asked by a client how they could easily add anchor links within a particularly long post they had made in WordPress. (Ie. a list of links at the top of the page that would jump the user down to the appropriate section where they needed to be.) I thought about maybe explaining to them to switch over the HTML tab and writing the HTML for the anchor links, but that step alone can sometimes really freak some clients out. :) So I came up with a solution to the problem using shortcodes and using the "build link" button already in the WordPress edit post box.

So the first step is to create the code so the shortcode will work. If you don't already have a functions.php file in your template directory, make a new file, name it "functions.php" and add the following code (surrounded by <?php and ?>). If you do already have a functions.php file, add the following code within those php tags:

function anchorlink($atts) {
extract(shortcode_atts(array(
"id" => ''
), $atts));
return '<a name="'.$id.'"></a>';
}
add_shortcode('anchor', 'anchorlink');

Then, in your post. In front of the location you want to be able to create a link to add the following:

[anchor id="unique_id_here"]

So for example.. If your post is a "Q/A" type page, down where you have the full question and answer, add that shortcode:

[anchor id="elephantquestion"]Q: What do elephants eat?
A: Elephants eat mainly grass, leaves, bark and twigs. Sometimes they will also eat fruit and seeds.

Then at the top – where you probably have a list of all the questions on the page like this:

What do elephants eat?
What do giraffes eat?
etc.

(In this example) highlight the "What do elephants eat" text, click the LINK button in the edit post area – remove the "http://" that automatically is included in the link field that pops up and instead, add a pound symbol # and then the id you used:
Screen shot 2009-09-10 at Sep 10  11.51.47 AM

I know this isn't a huge leap from simply writing the HTML that does this. But I've found that some clients don't understand (and don't want to understand) HTML. This IS actually simpler for them. Especially when you might run into the scenario of someone forgetting to close the anchor tag with a </a> – and suddenly the whole page is acting weird… I also think it's probably helpful to SEE the ID right there in plain text in the post – so that when you're making the links at the top, you can quickly see what the ID is that you need to link to.

4 Responses to "Anchor Links in WordPress Posts – another shortcode solution"

1 | MindWireMedia

September 11th, 2009 at 8:50 pm

Avatar

Good article, you can also install the tiny MCE advanced plugin. Or if you are looking to have links generated automatically to heading tags in your content for automatic anchor link jumps check out the Auto Anchor List wordpress plugin http://www.mindwiremedia.net/products/wordpress/plugins/auto-anchor-list-wordpress-plugin-home/

2 | Matt

April 21st, 2010 at 11:44 am

Avatar

Hey Jennifer,

Great shortcode. Thank you!

I add an HTML non-breaking space to the anchor b/c IE wasn't jumping to the anchor. Other browsers were. go figure :)

return ' ';

3 | Wouter Meeuwisse

August 5th, 2010 at 6:06 am

Avatar

Great shortcode, thanks for sharing! just what i was looking for!

4 | David Radovanovic

August 12th, 2010 at 12:02 pm

Avatar

Thanks for sharing scriptygoddess!!

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements