Support » Fixing WordPress » Hyperlink to Permalink Anchor?

  • Resolved korereactor


    I’m guessing there’s an easy answer to this, but since I’m a WordPress newbie, I have to ask. How do I create a hyperlink outside the loop on the same page to a permalink anchor, such as the following:

    <?php permalink_anchor(‘type’); ?>

    I like to have “quicklinks” at the top of my page which link to my posts below. Any help would be appreciated. I’m currently trying to convert my template from Blogger. You can view my Blogger page at to get an idea of how I want the links to work in WordPress. Any help is much appreciated. Thanks!

Viewing 13 replies - 1 through 13 (of 13 total)
  • Is this about placing links to posts off menus, or setting up something like your latest posts?

    If the former, you can pass get_permalink() a post or Page ID to generate (that is, return) the permalink to it.

    If the latter, try this in your template(s) (change the ‘showposts’ value to set how many to display):

    <?php $latest = new WP_Query('showposts=8'); ?>
    <?php while($latest->have_posts()) : $latest->the_post(); ?>
    <li><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></li>
    <?php endwhile; ?>

    That’s what I’m using now, and that shows the latest posts like I want, however, I need the latest posts to link to the post which is actually on the same page, not like your normal permalink, which usually links to the post on another page. Basically I need to generate a hyperlink outside the Loop which directs the user to the specified post within the loop, but all on the same page.

    I’d also like to add that the hyperlink that I need to create that needs to refer to the anchor must be dynamic, and change when the posts change. I tried to use the following:

    <a href="#post-<?php the_ID(); ?>" target="_self"><?php the_title(); ?>...</a>

    Won’t work though, as the line needs to be out of the loop, and <?php the_ID(); ?> does not work out of the loop. If I could just bring up the Post ID outside the loop I’d have it made.

    Anyone? You can see an example of how I want my site to function here:

    Basically I need a dynamic hyperlink which corresponds to a dynamic anchor tag.

    Just a guess:
    <?php $posts = get_posts('numberposts=1'); foreach ($posts as $post) : ?>
    <a href="#post-<?php the_ID(); ?>" target="_self"><?php the_title(); ?>...</a>
    <?php endforeach; ?>

    adjust numberposts=1 to suit.

    IanD’s example will work in the WP_Query example above as well (the target attribute is not necessary). Just change:

    <a href="<?php the_permalink(); ?>" rel="bookmark">


    <a href="#post-<?php the_ID(); ?>"

    Your posts will need to have these unique identifiers; just add it to your title h2 tags:

    <h2 id="post-<?php the_ID(); ?>">

    Maybe I’m missing something, but that doesn’t work for me. I even copied the code above as is. The <?php the_ID(); ?> tag does not work when used there, at least not for me.

    Do you mean the id doesn’t display as the link (i.e. #post-10), or the anchor link doesn’t work on the page?

    Yes, the ID doesn’t display in the link. It just shows up as #post-.

    I’ve pounded on the code I provided and it works for me. If you could, paste the code from your template here:

    And reply back with the link you receive. We’ll go from there.

    Here it is. Hopefully I just made a simple mistake?

    *You* made no mistake. However, it appears we’ve found another bit of post data that isn’t available through get_posts() in the code as you have it (I’m starting to count these on two hands…). Change your get_posts() line to this:

    <?php $posts = get_posts('numberposts=10'); foreach ($posts as $post) : setup_postdata($post); ?>

    That will make it work. Also note that the non-get_posts() code I listed way above does as well.

    Perfect. Whew! Just when I thought I was going to have to stick with Blogger! Thanks loads!!! Works like a champ!! I’m loving WordPress!!

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Hyperlink to Permalink Anchor?’ is closed to new replies.