spot_img

Get Permalink of Page Outside of The Loop in WordPress

WordPress users occasionally encounter situations where they need to obtain the URL or permalink of the main page since there are several loops on the page.

Let’s look at how to obtain the permalink for a page that is not inside of a loop in WordPress.

The Problem

You are undoubtedly already familiar with WordPress get_permalink() function. This method is used to retrieve the URL of the active item within the loop.

Get permalink(), however, won’t get the URL we want if we need to obtain the link to the page that the loop is located on.

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().

get_permalink(get_queried_object_id());

Let’s examine what is going on in this situation. The ID of the page we are presently on will be obtained by calling get_queried_object_id() first. In order to acquire the URL we want, we send this ID value to the get_permalink()  method.

Naturally, if we didn’t require the URL, we could get_queried_object_id() to obtain the object ID only, which we could then utilize as necessary.

I’m done now. You are now aware of how to obtain the WordPress permalink for a page that is not inside of a loop. Coding is fun!

spot_img

Subscribe

Related articles

OnePlus 5T Wallpapers Download

Introduction: The OnePlus 5T is a popular smartphone known for...

Airtel’s First Quarterly Loss in 2002: A Closer Look at Jio’s Impact

The telecom industry has witnessed several significant shifts over...

Xiaomi Confirms Investment in Blackshark Gaming Phone Launch set for April 13

An engaging introduction to Xiaomi Confirms Investment in Blackshark...

LG G7 ThinQ M LCD Panel

Introduction:The LG G7 ThinQ M LCD panel is a...

Intel Core i9 Laptops with Optane Memory

Intel Core i9 laptops with Optane Memory combine the...

Apple iOS 11.4 Beta 1

Apple iOS 11.4 Beta 1 is the latest update...

Google Search AI Reorganization: Improving Search Quality and User Experience

Introduction:In the ever-evolving digital landscape, search engines play a...
Peter Graham
Peter Grahamhttp://fix-iphones.com
Hi there! I'm Peter, a software engineer and tech enthusiast with over 10 years of experience in the field. I have a passion for sharing my knowledge and helping others understand the latest developments in the tech world. When I'm not coding, you can find me hiking or trying out the latest gadgets.

LEAVE A REPLY

Please enter your comment!
Please enter your name here