FAQ

Frequently Asked Questions

How to: Get permalink outside of the loop – CodeRevolution

Some days ago, rebellion, one of CodeRevolution 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!

CodeRevolution Knowledge Base

Video tutorials