How To Get URL Of Current Page or Post In WordPress

WordPress has grown to be one of the most widely used website management systems in the modern era. When working in WordPress, you may occasionally need to quickly obtain the URL of the current page or even a specific post.

Occasionally, it’s difficult to locate the appropriate feature that will enable you to easily obtain a URL in WordPress. Thus, how do you get the URL of the current page or post in WordPress?

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.

In WordPress, finding the right combination that lets you do exactly what you want can be difficult at times. As coding languages evolve, so do the functions that make it possible for you to come up with the best solution with the fewest number of lines of code possible.

Don’t stress if you can’t remember the URL of the current post or page in WordPress. Find out how to quickly obtain the URL you’re looking for in the following paragraphs.

How To Get The URL Of The Current Page In WordPress?

In order to create a website to your specifications, WordPress has been designed to be as simple as possible. It’s not always easy to find the right code, despite the simplicity of the system.

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:

<?php $current_url=”http://”.$_SERVER[‘HTTP_HOST’].$_SE

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:

How To Get URL Of Current Page or Post In WordPress?
get_page_link (int | WP_Post $ post = false, bool $ leavename = false, bool $ sample = false) 
How To Get URL Of Current Page or Post In WordPress?

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:

function get_nopaging_url()

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.

How To Get The URL Of The Current Post In WordPress

Once you’ve learned how to get the current page’s URL, let’s go one step further. A little more precision is required when attempting to retrieve the URL of the current post, and this usually entails adding the word post to the code.

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 the 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.

How To Get URL Of Current Page or Post In WordPress?

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.

How To Get URL Of Current Page or Post In WordPress?

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.

Wrap Up

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.

Further Read:

  1. How To Hide Page Title In WordPress Without Plugin
  2. How to Fix Err_SSL_Protocol_Error Quickly
  3. How To Completely Remove Elementor From WordPress
  4. How To Remove Duplicate Title Tags In WordPress