0

As already known that the function the_permalink echos out the URL for current post in the loop, while the function get_permalink returns the permalink for a specific post using $post_id, but get_permalink has the $leavename that allows return the URL structure of a post to finally (for example) get a URL that looks like this http://localhost/%postname%/. So, When do i may need such output?

Please notice the output:

   <?php 
        $x= get_permalink($post_id, true);
        echo $x;
        //output
        //http://localhost/%postname%/
    ?>

If i turned the $leavename to false i will already have the post name in the URL. So again , What makes me turn it to true

5
  • I haven't seen anyone use that argument to get the permalink until now. It's mainly used simply as get_permalink(). It's an optional value to decide whether to keep the post's name or not.
    – Johansson
    Feb 14, 2017 at 8:46
  • but it doesn't output the post name, It outputs the Structure Tag for the post's name. Feb 14, 2017 at 8:48
  • According to WordPress developers, it will decide to keep the name or not. Expand the code and notice the line 126 and 130. The $leavevalue is equal to %postname%
    – Johansson
    Feb 14, 2017 at 8:51
  • I have made an update, please notice the result of the added code, and tell me how to make use of such output? Feb 14, 2017 at 8:53
  • I don't think that you would need to use true unless for developing purposes. As @Milo mentioned, the URL preview mechanism (or some plugins) use that function to update the slug based on the original post's name. You don't need it for theme development.
    – Johansson
    Feb 14, 2017 at 9:12

1 Answer 1

2

It's used in admin context to display URL previews. When you change the slug of a post, the code that enables that mechanism is getting the format of the URL via that function.

2
  • Would you please consider the update made to the question Feb 14, 2017 at 8:55
  • When you edit a post, there is a field that lets you change the slug, it uses Javascript to accomplish this. It gets the structure of the URL independent of the actual URL, so it can provide an interface for changing the URL. Consider that WP's internal code uses its own API, you will likely never use many if the API's features, simply because its use is specific to internal needs and not general themes or plugins.
    – Milo
    Feb 14, 2017 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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