Post Pic

How to: Get permalink outside of the loop

Some days ago, rebellion, one of WpRecipes readers, asked me how to access post data outside of the WordPress loop. I have provided a solution to him, however it wasn’t possible to get a permalink outside of the loop. Here’s how to do it easily.

To get a permalink outside of the loop, you have to use the get_permalink() function. That function takes a single argument, which is the ID of the post you'd like to get the permalink:

<a href="<?php echo get_permalink(10); ?>" >Read the article</a>

You can also use this function with the $post global variable:

<a href="<?php echo get_permalink($post->ID); ?>" >Read the article</a>

Thanks to Julien Chauvin for the tip!

2 Responses

Aug 24 2011 20:32

Thanks for the tip! How do you get the permalink on non-singular pages (i.e. tags, categories, archives, blog etc.)? If you can add this info to this tip, it would make it complete. Thanks!

Nov 10 2011 14:25

Hello guys,

How to get page/post permalink in the head?

( i wanna place some conditionals to add canonical tag so i need to get the pages permalinks in the head – so outside the loop too ).

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required