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.

Related posts:

  1. Simple Shortcode for Line Breaks One kind of funny thing with WordPress is that entering...
  2. Adding a block of HTML to a WordPress post One problem with the WYSIWYG editor in WordPress is that...
  3. Custom Template for Parent and Sub Categories (without a plugin) in WordPress When using WordPress as a CMS, I often repurpose the...
  4. SEO Plugins and problems with the titles not rewriting (WordPress) I can't speak for anyone else who has run into...
  5. Striping IMG tags from the_content in WordPress (and how to fudge page excerpts) Background: For a site I was working on, I was...

Related posts brought to you by Yet Another Related Posts Plugin.

1 Response 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/

Comment Form

Featured Sponsors


  • Michael: You can use get_header(2) in your case. The filename of your custom header has to be header-2.php. The problem with include(your_file.php) is, all
  • cliff: hi wonder if you can help me, pls i designed www.kouga.mobi using dreamweaver CS3 and now want to make the phonenumbers into links so that if you
  • jerey: how do i rewrite this because it tried RewriteEngine on #Options +FollowSymlinks RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FIL

About


Advertisements