WordPress has become one of the most popular website management software in modern times. When working in WordPress, you will sometimes need to get the URL of the current page or even a specific post in WordPress in just a few seconds. Sometimes it’s not that easy to find the right feature that will allow you to get a URL in WordPress easily. So how to get the URL of the current page or post in WordPress?
To get the URL of the current page in WordPress in the code, you can put several functions, but the most common is definitely get_permalink (). If you want to get the URL of the current post, you can do so by adding the get_permalink function (int | WP_Post $ post, bool $ leave name = false in your code.
Sometimes it is really challenging to find the right combination in WordPress that will allow you to do exactly what you need. Programming languages are constantly changing, as well as functions that will allow you to come up with the right solution with as few lines of code as possible. If you are struggling to get the URL of the current page or post in WordPress, don’t worry. Read below for ways to get the URL you need easily.
How to get the URL of the current page in WordPress?
WordPress is designed to make a website to your liking in the simplest way possible. Despite the simplicity, it is sometimes difficult to find the right code. There are several different functions, ie, lines of code, to get the URL of the current page in WordPress. You can do this with or without pagination. Which function is best to use will depend on what you need the URL link for and how you will use it next.
If you want to get a URL of the current page in WordPress with pagination, there are several ways to do it. The most commonly used line of code that will successfully execute this request is definitely get_permalink (). This feature will successfully give you the URL of the page but will only work within the loop.
If you want to get the current URL with http part of the URL use the following function:
If you want the current page URL not to contain the http part of the URL add the function:
$ url = home_url ($ url);
Another way to easily get the current page URL in WordPress is the simple line of code that you can use in any PHP template file on the WordPress website. The line of code is:
global $ wp;
$ current_url = home_url ($ wp-> request);
You can also use this option by adding the following line of code:
global $ wp;
$ pos = strpos ($ current_url, ‘/ page’);
The official advice given by WordPress.org is to use this line of code to get a permalink of the current page or page ID:
get_page_link (int | WP_Post $ post = false, bool $ leavename = false, bool $ sample = false)
If you want to get the URL of the current page without pagination, you will need another line of code that will extract the page and the page number you need. You can do this using the following function:
function get_nopaging_url ().
If you want to get the current URL without any additional parameters, including pagination, you can use this code:
$ current_url = strtok ($ _ SERVER [“REQUEST_URI”], ‘?’);
Isitwp has also created its own line of code that allows you to achieve what you need with just one code line. All you need to do to get the current page URL is to add this line of code in the theme’s index.php:
<? PHP $ paged = (get_query_var (‘paged’))? get_query_var (‘paged’): 1; ?>
In addition, Isitwp offers you additional tips on how to do it in the simplest way. If you are adding code in WordPress for the first time, be sure to read the tips offered by Isitwp.
As you can see, there are several ways to get the URL of the current page in WordPress. To make sure you find the right code, we have offered you a number of features, and you choose the one that will best suit your needs.
How to get the URL of the current post in WordPress?
Now that you know how to get the URL of the current page, let’s go a step further. When you want to get the URL of the current post, you will need to be a little more accurate, which will mostly require you to add the word post to the line of code. One way you can get a link to the current post in WordPress is to enter a line of code:
get_permalink (int | WP_Post $ post, bool $ leave name = false
This line of code will allow you to retrieve the full permalink of the current post or post ID. This feature will work great if you want to get a link to the post. However, this feature will not work for all posts. For example, if it is a date archive or the latest post page, this function will not work. In that case, it is better to use this code:
global $ wp;
echo home_url ($ wp-> request);
If you want to extract a single post from the archive, you can use get_post_type_archive (). This code will allow you to very easily and quickly get a post from the archive when get_permalink is not working.
If for some reason, you need to get the current page number for paginated posts, you will need to add a function that will be directed to the page number in addition to the URL. You can do this by adding this line of code:
<? PHP echo ‘(Page’. $ page. ‘of’. $ num pages. ‘)’; ?>