WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Hyperlink to Permalink Anchor? (14 posts)

  1. korereactor
    Member
    Posted 9 years ago #

    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 http://www.korereactor.com to get an idea of how I want the links to work in WordPress. Any help is much appreciated. Thanks!

  2. Kafkaesqui

    Posted 9 years ago #

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

    <ul>
    <?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; ?>
    </ul>

  3. korereactor
    Member
    Posted 9 years ago #

    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.

  4. korereactor
    Member
    Posted 9 years ago #

    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.

  5. korereactor
    Member
    Posted 9 years ago #

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

    http://www.korereactor.com

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

  6. iand
    Member
    Posted 9 years ago #

    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.

  7. Kafkaesqui

    Posted 9 years ago #

    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">

    to:

    <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(); ?>">

  8. korereactor
    Member
    Posted 9 years ago #

    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.

  9. Kafkaesqui

    Posted 9 years ago #

    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?

  10. korereactor
    Member
    Posted 9 years ago #

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

  11. Kafkaesqui

    Posted 9 years ago #

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

    http://pastebin.ca/

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

  12. korereactor
    Member
    Posted 9 years ago #

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

    http://pastebin.ca/15813

  13. Kafkaesqui

    Posted 9 years ago #

    *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.

  14. korereactor
    Member
    Posted 9 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.