Meks
Do you know you can try any of our premium WordPress themes for free?Show me the themes
Get current page URL in WordPress

Get current page URL in WordPress

85Shares

Here is a handy snippet with which you can easily get the current page URL on your WordPress website regardless of the page which is being displayed. It doesn’t matter if you need the URL for single post, page URL, the home page, category template, tag template, custom post type or any other WordPress template.

You can use this piece of code inside any PHP template file on your WordPress website:

And if you want WordPress to get current page slug only (last part without your base URL), here is the code:

Bonus: Get current URL in WordPress on specific PHP templates

As an alternative way (not an all-in-one solution), WordPress has its own functions which you can utilize depending on a page that is currently being displayed. Let’s take a look at the examples below.

Top WP Hosting recommended by Meks

For a single post or a page URL, if single.php or page.php theme template file is currently loaded:

For current taxonomy term (eg. category or tag) URL, if taxonomy.php, category.php, tag.php etc… theme template file is currently loaded:

For current author archive, if author.php template is used:

For homepage URL, there is a function which returns the same URL on all pages, not only in front-page.php or home.php template:

That’s it, feel free to try!

85Shares
Bojan Petrovic

Co-founder and WordPress developer at Meks. He has been involved in the business since 2009. creating themes, plugins and services on top of WordPress, continuously aiming to choose best approaches and work smarter, not harder.

Try theme now for free!
  • Access to a copy of the demo website
  • Full access to the theme options
  • No credit card required

* We will send you an occasional newsletter with special offers, news & theme updates but you can unsubscribe at any time.

Meks