Support » Fixing WordPress » Get current URL code?

  • Resolved bremmerm


    Is there a php code so that if someone visits the page the code can automatically get the url to that page?

    I am trying to create dynamic page titles but the pages that are being created are dynamic as well. What I am trying to tell it to do is get the current url, example:

    Then it would work backwards taking “dallas” then “tx” to create a dynamic page title and description:

    Dallas, TX Basketball Tournaments

    Find Basketball tournaments in Dallas, TX. Join and participate in basketball leagues throughout the city of Dallas.

    Whats in bold would pull from the URL. Ooohh any help would be great!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Michael


    Forum Moderator

    Awww i typed nearly that same keywords in and i guess i wasn’t savvy enough to find anything.

    I thought maybe there was something more wordpress oriented. Thanks for your help 🙂 .

    OKay….so i now know how to find the URL…

    How would I then find the state abbreviation and the city in the URL:

    (keep in mind it could be any state and city!)

    I guess in non coding words I would tell it to remove – because that will be consistent. Then to take the next two letters and store them in (state). Then skip the “/” then take the next word and store them in (city) :

    (City) , (State) Basketball Tournaments

    Find Basketball tournaments in (City), (State). Join and participate in basketball leagues throughout the city of (City).

    I have no idea what to type in google to find this solution



    Forum Moderator

    if you used the link you would have your url returned with this function:

    then you could do a bit of string and array manipulation (assuming that your url structure is always as in your example:
    no extra slash at the end or anything after the town)

    $bits = explode('/',curPageURL());
    $bit_nr = count($bits);
    $city = $bits[$bit_nr-1];
    $state = $bits[$bit_nr-2];

    In order to have a URL, such as /search/state/town/ , rules will already exist that map that query into real query variables, if you know what they are, then why not just check them?

    For the sake of illustrating what i mean, for example the existing query variables might be..


    ..or whatever, my point being, there are already some existing query variables (there should/must be), unless the URL in question is a folder (but then it wouldn’t be a dynamic URL/query if that were the case).

    I’m just saying, you might be overthinking this… 😉 If the URL you’re looking at a URL isn’t a real file or folder, then it already has query variable.

    TIP: Do a few print_r’s and see what vars are present on your given page, eg. <?php print '<pre>'; print_r( $_GET ); print '</pre>'; ?>.

    Thanks guys


    Yes I see what you mean and after looking at it it does have a variable. I will try what you suggested and let you know if it works.

    The codes I found were:


    I ended up having to use alchymyth method. THanks for the code that worked great.

    Another problem. I only want that type of title to show only on those pages. So I created the following:

      $shortdesc = myTruncate2(curPageURL(), 46);
    $bits = explode('/',curPageURL());
    $bit_nr = count($bits);
    $city = $bits[$bit_nr-1];
    $state = $bits[$bit_nr-2];
      if ($shortdesc = "") {
    print 'Sports Leagues in '; print_r( $city ); print ',  '; print_r( $state );
    else {
    wp_title('&laquo;', true, 'right');

    The meta title show’s up great for the pages I want, but it also does the same thing for every page.

    The site in question is:

    An example page that looks great is:

    I must have something wrong with the code above…any help?




    Forum Moderator

    try to use == in the if statement:

    if ($shortdesc == "")

    simple simple fix 🙂 Thank you alchymyth and everyone for your help. I will indeed have more questions in the future. But this one in particular is solved

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