Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In WordPress I am using both the_permalink() and get_permalink() functions, but I cannot get any difference in the output of both functions. What is the difference between both functions?

share|improve this question

3 Answers 3

up vote 10 down vote accepted

the_permalink echos out the permalink of the current post to the frontend.

get_permalink however returns it as a variable, but does not echo it out. You can also pass a post ID to it if you want the permalink of another post.

the_permalink is equivalent to:

echo get_permalink();

Which is very close to what it actually does. This is the implementation of the_permalink:

function the_permalink() {
    echo esc_url( apply_filters( 'the_permalink', get_permalink() ) );
}
share|improve this answer
3  
It means all the functions like the_ID() and get_ID() also have same differences like this. Sorry but i am new to wordpress that's why asking –  AliasgerSW Jun 27 at 11:52
    
Thank Tom J Nowell. –  AliasgerSW Jun 27 at 11:57
    
@AliasgerSW thats the pattern, there are some exceptions, but generally it is that way –  ialocin Jun 27 at 12:07
    
@AliasgerSW when in doubt, look at the code. QueryPosts is a great site for looking up the source code of WordPress functions. –  bungeshea Jul 3 at 22:22
    
Hey thanks @bungeshea, its a great website. –  AliasgerSW Jul 4 at 4:51

If you look at the WordPress Codex on this you will see that get_permalink() is there for use outside the loop. the_permalink() is for use within the loop. That’s the easiest way to look at it.

share|improve this answer
    
Ok.. Thanks Matt Royal –  AliasgerSW Jun 27 at 11:56
    
might be the easy way to look at it, but not a very complete one –  ialocin Jun 27 at 12:05
    
For sure, @Tom J Nowell♦ answer is much more concise. Much better and well explained :-) –  Matt Royal Jun 27 at 13:29

the_permalink() is used in posts loops, like the_title(). Read more about loops in The Loop.

get_permalink() can be used in loops or outside the loops. In the loops, the function returns (not echo) the current post permalink. But outside loops, it requires a post ID.

For example:

echo get_permalink( 1 );

This will display the current page permalink:

echo get_permalink();
share|improve this answer
1  
actually it doesn't require an ID outside the loop –  ialocin Jun 27 at 12:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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