permalinks - Difference between the_permalink() and get_permalink() function

admin2025-06-03  3

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?

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 edited Jun 27, 2014 at 16:24 Peter Mortensen 2682 silver badges10 bronze badges asked Jun 27, 2014 at 11:36 AliasgerSWAliasgerSW 1531 gold badge2 silver badges8 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 23

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() ) );
}

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.

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();
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748925709a314874.html

最新回复(0)