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 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!

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.

55 comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You don't need to have loads of money to get a world-class WordPress website! All our themes are rated with the highest 5-star rating. Have a look and choose the best one for you.
Meks WordPress Themes
Meks