0

I am trying to get the current page url using this code.

global $wp; $current_url = home_url( add_query_arg( array(), $wp->request ) );

It works fine. But when default permalink settings (Plain) are in effect, it doesn't work.

How do I get this to work?

Thank you.

2
  • Why not use get_the_permalink()?
    – Den Isahac
    Jul 16, 2017 at 23:22
  • As far I understand that should be used inside the loop. I want something that I can use outside the loop. Jul 16, 2017 at 23:23

1 Answer 1

0

If you want to display the current permalink outside The Loop, then do the following:

  • Access the global $post variable
  • get_the_permalink($post->ID)

Youf code as follows:

global $post;

echo get_the_permalink($post->ID);
2
  • The $post global is often polluted by other code, get_queried_object() would be safer in this case.
    – Milo
    Jul 16, 2017 at 23:37
  • Hi Milo, Can you please elaborate a bit on that? Jul 17, 2017 at 2:00

Your Answer

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

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