0

Is there an easy way to link to a specific page/post outside the loop? I used the get_permalink() code as follows.

<a href="<?php echo get_permalink( 164 ); ?>/post-slug/"> </a>

But this meant that I had to get the post ID from the Shortlink in the Admin for the post I wanted and then append the slug to it. Is there a better way to get the post ID of the post I am looking for, without using the loop?

My permalinks are set up to use the post name so this seemed to work.

  • 1
    You should not have to append the post-slug to the get_permalink function. Check the codex - if the permalink is not returning anything - make sure the ID is correct. codex.wordpress.org/Function_Reference/get_permalink – gdaniel Feb 10 '14 at 23:01
  • I am still really confused here. You are correct that I don't need to append the post-slug here. My question is can I get the address for the post without knowing the ID? And if I need the ID, can I get it without being in a loop? – user45758 Feb 12 '14 at 16:53
1

Have you tried the get_page_by_title function?

You can use like <?php echo get_permalink(get_page_by_title('My Post Title')); ?>

  • +1 not for the easiness the OP asks for, but for uniqueness. – kaiser Feb 11 '14 at 0:40
  • The drawback is that this will run an additional query and one that may be avoidable. – s_ha_dum Feb 11 '14 at 2:11
  • I am afraid that this didn't work for me. The get_permalink returned the address of the current post whereas I wanted an address of a different post. My permalinks are set up to use the post name and I want a link for a different post. – user45758 Feb 12 '14 at 17:01
  • I want to be able to jump around the website to different pages and posts regardless of where I am on the site. What is the easiest way to link to a specific post or page? – user45758 Feb 12 '14 at 17:04
  • How did you use the get_permalink function? Are you linking to a post? I forgot to mention, if you are linking to a post you have to do get_page_by_title('Title', OBJECT, 'post') – czerspalace Feb 13 '14 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.