Get Permalink of Page Outside of The Loop in WordPress

Wordpress Featured

When you are working in WordPress, sometimes you run into a situation where you have multiple loops on a page, and you need to get the main page’s URL or permalink. Let’s take a look at how to get a permalink of the page outside of the loop in WordPress.

The Problem

You are probably familiar with the get_permalink() function in WordPress. We use this function to get the URL of the current object inside the loop. However, if we need to get the link of the page where the loop is inside, get_permalink() will not give back the URL we need. 

The Solution

To get the URL of the page itself, we need another function. The new function we need is get_queried_object_id()and we will use it together with get_permalink().


Let’s take a look at what’s happening here. First, get_queried_object_id() will get the ID of the page we are currently on. We pass this ID value into the get_permalink() function, retrieving the URL we need. Of course, if we didn’t need the URL, we can use get_queried_object_id() to get the object ID alone and then use that however we needed. 

That’s it. Now you know how to get the permalink of the page outside of the loop in WordPress. Happy Coding!

Disclaimer: Some posts on Techozu may contain affiliate links. Purchasing through these links may earn us an affiliate commission.