This ia a very quick note on the right way to get the URL of the current page or post with PHP in your WordPress template:
<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>
This will work for both posts and pages and outside the loop. I am using this for things like social media sharing buttons for example. Use it anywhere in your WordPress template to get the URL. Use the_permalink() only for posts and within the loop!
Hope this helps someone else wondering why the_permalink() is displaying abnormally when used outside the loop and for pages






Please share or recommend this page: