How to Get URL of Current WordPress Page or Post?

We often need to know how to get url of current wordpress page or post if we re-programming wordpress. Here I will share 2 methods with you. One is via PHP code, and the other is using wordpress function.

First, let me intruduce how to use PHP to achieve our demands. We can add the code to suitable pages, say footer.php. Go to your wordpress template, and edit footer.php. Here we use http://kingphp.com/15.html as an example.
<?php echo $_SERVER[“SERVER_NAME”]; echo ” ?>

You will get  kingphp.com;

<?php echo $_SERVER[“REQUEST_URI”]; ?>

You will get /15.html

<?php $current_page_URL = $_SERVER[“SERVER_NAME”] . $_SERVER[“REQUEST_URI”]; echo $current_page_URL;   ?>

You will get kingphp.com/15.html

Usually we use:

<?php $current_page_URL =(isset($_SERVER[“HTTPS”]) && $_SERVER[“HTTPS”]==”on”) ? “https://” : “http://”; $current_page_URL = $_SERVER[“SERVER_NAME”] . $_SERVER[“REQUEST_URI”]; echo $current_page_URL;   ?>

Method Two, we can use wordpress function: the_permalink()

This function can show full url of a page or post.

King Php Science Welcome, you are from: 54.158.25.146