Support » Developing with WordPress » Get the current URL

  • Hi guys.
    I need to know what is the function that returns the current URL. I used get_permalink() but this don’t work with the main page, the categories page, search… and other pages, only works for the posts pages.


Viewing 5 replies - 1 through 5 (of 5 total)
  • <?php echo $_SERVER['PHP_SELF'] ?>

    If you want something else, then google “php get current url” to find the code snippet you need.

    Thanks, the correct code is:

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

    You can use get_permalink() outside the loop with an ID as a parameter:

    <?php echo get_permalink( $post->ID ); ?>

    If you are trying to detect if a user is viewing the home page, you can use is_home(), which is a WordPress specific function. In a conditional statement it would be used like this:

    // code for home page goes here

    I use this on my blog If you look at the home page, I have an about us box on the right side. On all the other pages, I hide the box and display Google Ads.

    You can use one of the following for other pages too:

    is_category() – Use this if you want to detect if a user is viewing a category page. Or if you want to detect a specific category you can use something like is_category(‘SEO Tips’) for a category called ‘SEO Tips’.

    is_single() – If user is viewing a post page. For a specific post use is_single(17) for a post with an id of 17. Or you can use is_single(‘WordPress Tips’) of a post with the title ‘WordPress Tips’.

    Here is a complete list of conditional tags here:

    try : <?php the_guid($id); ?>

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get the current URL’ is closed to new replies.