Home > CMS Tutorial > WordPress > body text

How to get the current page URL in WordPress

angryTom
Release: 2019-11-16 15:21:37
Original
5061 people have browsed it

How to get the current page URL in WordPress

How does WordPress get the current page URL address

We often need to get the current page URL when making WordPress templates Address, here I will introduce to you how to get the URL address of the current page.

(Recommended tutorial: WordPress Tutorial)

1. Use WordPress To achieve it with native functions, the code is as follows:

$current_url = home_url(add_query_arg(array()));
Copy after login

2. A universally applicable method, the code is as follows:

$current_url = home_url(add_query_arg(array(),$wp->request));
Copy after login

3. Directly in WordPress Add the following code:

< ? php
// 说明:获取完整URL 
function curPageURL() {
    $pageURL = &#39;http&#39;;
    if ($_SERVER["HTTPS"] == "on") {
        $pageURL. = "s";
    }
    $pageURL. = "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL. = $_SERVER["SERVER_NAME"].
        ":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL. = $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}
?>
Copy after login

and then call it as follows, the code is as follows:

<?php 
    echo curPageURL(); 
?>
Copy after login

The above is the detailed content of How to get the current page URL in WordPress. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!