Get current page URL in WordPress

wordpress

Here is a handy snippet with which you can easily get 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, 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 all-in-one solution), WordPress has its own functions which you can utilize depending on a page which is currently being displayed. Let’s take a look at the examples below.

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!

0938.54.84.99