Introduction
Are you looking for an effective method to get the current page URL in the WordPress site? If your answer is yes, this blog today will show you a useful solution to do that. This method we are going to introduce to you will help you get not only blog URL or post URL but also home page, tag template, custom post form, and much more. So, let’s get started now!
How to get current page URL in WordPress easily
First of all, on the dashboard, you need to go to Appearance -> Theme Editor. Then, choose the theme files you want and add the code below:
You can get the URL of any PHP template file on your site by using the following code:
global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) );
In case you have an intention to get the latest slug page, you just need to utilize the code:
global $wp;
$current_slug = add_query_arg( array(), $wp->request );
Besides that, if you need to get current URL Function, here is the code you want to use:
<?php
function wp_get_current_url() {
return home_url( $_SERVER['REQUEST_URI'] );
}
Moreover, for a single post or URL page, whenever the single.php or page.php theme template file is loaded:
$obj_id = get_queried_object_id();
$current_url = get_permalink( $obj_id );
For category or tag URL which is known as taxonomy term, let’s see the category.php or tag.php theme template is loaded:
$obj_id = get_queried_object_id();
$current_url = get_term_link( $obj_id );
If you want to use the author.php template for the current author archive:
$obj_id = get_queried_object_id();
$current_url = get_author_posts_url( $obj_id );
Finally, if you are looking for a feature that returns the same URL on all pages for homepage URL, let’s explore:
$current_url = home_url( '/' );
Conclusion
All in all, we have already shared with you some codes that can help you get current page URL in WordPress effectively. Therefore, we hope that this blog will be handy assistance for you in building and developing a WordPress website. If you think it may be helpful for your friends, don’t hesitate to share it. What’s more? We are providing a lot of Free WordPress Themes on various topics. If you are interested in engaging and impressive themes, don’t miss them.