How To Get URL Of Current Page or Post In WordPress
When working in WordPress, you may occasionally need to quickly obtain the URL of the current page or even a specific post.
If you are new to WordPress, you may have difficulty obtaining the URL for the current page or post.
You can use several functions in WordPress code to get the URL of the current page, but the most common is undoubtedly get_permalink(). If you want to get the current post’s URL, add the get_permalink function (int | WP Post $ post, bool $ leave name = false) to your code.
I’ve written a detailed guide below on how to find the URL of the current page or post. You can find the URL you want by following the steps outlined below.
Steps To Get WordPress Current Page URL
The URL of the current page in WordPress can be retrieved using a number of different functions, i.e. lines of code. This can be done with or without page numbers. What you need the URL link for and how you intend to use it will determine which function to use.
Getting the current page’s URL in WordPress with pagination can be accomplished in a variety of different ways. The get permalink function is unquestionably the most frequently used line of code for successfully completing this request ().
This feature will successfully provide you with the URL of the page, but it will only work if you are in the loop at the time of use.
To obtain the current URL, including the HTTP part of the URL, use the following function:
If you do not want the current page URL to include the HTTP part of the URL, you can use the following function:
$ url = home_url ($ url);
Using a simple line of PHP code, you can easily find your current page’s URL in WordPress without having to manually type it in. It’s the following line of code:
global $ wp; $ current_url = home_url ($ wp-> request);
Alternatively, you can use the following line of code to achieve the same result:
global $ wp; $ pos = strpos ($ current_url, ‘/ page’);
In order to obtain a permalink for the current page, WordPress recommends the following line of code:
get_page_link (int | WP_Post $ post = false, bool $ leavename = false, bool $ sample = false)
The URL of the current page without having to go through the pagination process will require another line of code that extracts the page and the page number you require from that URL. This can be accomplished through the use of the following function:
In order to obtain a copy of a website’s current URL without any additional parameters, including pagination, the following code can be used:
$current_url = strtok ($ _ SERVER [“REQUEST_URI”], ‘?’);
Alternatively, you can also add the below line of code in index.php that allows you to get the URL in just one line of code.
<?PHP $paged = (get_query_var (‘paged’))? get_query_var (‘paged’): 1;?>
The URL of the current page in WordPress can be obtained in a variety of ways, as you can see above. You can choose from a variety of features to ensure that the right code is found; simply select the feature that best suits your needs.
Steps To Get URL Of Current Post In WordPress
Once you’ve learned how to get the current page’s URL, let’s go one step further. In WordPress, you can get a link to the current post by entering a single line of code:
get_permalink (int | WP_Post $ post, bool $ leave name = false
If you include this line of code in your code, you will be able to retrieve the complete permalink for the currently active post or post ID. If you want to receive a link to a particular post, this feature will be extremely useful.
This feature, however, will not be available for all posts. This function will not function, for example, if the page is a date archive page or the page with the most recent posts. In that case, it is preferable to make use of the following code:
global $ wp; echo home_url ($ wp-> request);
The get post type archive function can be used to extract a single post from a large archive of posts (). When they get permalink function does not work, this code will allow you to very easily and quickly retrieve a post from the archive.
It is possible that you will need to get the current page number for paginated posts for some reason; in this case, it will be necessary to add a function that will be directed to the page number instead of the URL. You can accomplish this by including the following line of code:
<? PHP echo ‘(Page’. $ page. ‘of’. $ num pages. ‘)’; ?>
Using The WordPress Dashboard
If you are new to WordPress, you can also see the URL of the current page or post from the WordPress Dashboard.
If you’re working on a new post, open your website in a new tab and then navigate to the post or page option in your WordPress Dashboard.
There, the current page or post will be marked as Draft. Roll your mouse over the preview button or right-click on it and select to open it in a new tab.
This will open the current post or page in a new tab with a preview URL associated with it. However, if you want to know what the future URL of your current post or page will be, press Ctrl + S once you have finished writing the new post or page.
Now, in your block editor’s right pane, select the post and then expand the Permalink tab. The URL of your current Post or Page will be displayed.
The URL under View Post Title is the URL of your current WordPress post or page. And, as shown in the preceding image, you can change it to suit your needs in the URL slug box.
I hope you learned how to get the URL of the current page in WordPress as well as the URL of the current post in WordPress.
I’ve given you two options. If you do not see the URL in your dashboard, you will need to add code to your WordPress. Another option is to view the URL directly in the dashboard if you’re using the block editor.
Let me know in the comment section if you are still facing any issues I will be happy to help you as soon as possible.