request))", etc."/> request))", etc.">

Home  >  Article  >  CMS Tutorial  >  How to get the current page URL in WordPress

How to get the current page URL in WordPress

angryTom
angryTomOriginal
2019-11-16 15:21:375081browse

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

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

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

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;
}
?>

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

<?php 
    echo curPageURL(); 
?>

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!

Statement:
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