I have created a page template where I need current page URL
I have tried
get_site_url();
and
get_permalink();
but it shows nothing.
Let me know is anything missing in code.
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI have created a page template where I need current page URL
I have tried
get_site_url();
and
get_permalink();
but it shows nothing.
Let me know is anything missing in code.
if you're using a page template, then if you want to get the current page URL you could try to use this function:
get_permalink();
or
echo get_permalink();
if you want to print the result
You can get page url like this.
global $post;
echo get_permalink( $post->ID );
Hope it helps :)
$post
may be overwritten by a secondary query, in this case get_queried_object()
is safer and avoids accessing globals directly.
– Milo
Jul 30 '16 at 15:40
$post
, that's how the_post
works. Use API functions when available rather than relying on global variables.
– Milo
Aug 1 '16 at 14:49