Thursday, November 10, 2022

Detect current URL and match with home page in wordpres

Detect current URL and match with home page in wordpres:

Sometimes you may select "Your latest posts" in backend settings->reading.

In this case your home page will be your theme's index.php file.

In this case you might need to detect what is your front page / home page and match to your current page.

In this case you can use below code to differentiate home page to other pages/posts:

$home_url = get_option('home') . '/';
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on'
? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

if ($home_url === $actual_link) {
    // Code goes here
}

// Happy coding...

No comments:

Post a Comment

Suomen kieli (Imperfekti)

In Finnish, the imperfekti is the past tense that describes actions that happened in the past, like the English simple past (e.g., "I ...