WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get current URL code? (10 posts)

  1. bremmerm
    Member
    Posted 4 years ago #

    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: http://www.examplesite.com/search/tx/dallas

    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!
    Thanks!

  2. alchymyth
    Forum Moderator
    Posted 4 years ago #

  3. bremmerm
    Member
    Posted 4 years ago #

    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 :) .

  4. bremmerm
    Member
    Posted 4 years ago #

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

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

    http://www.examplesite.com/search/tx/dallas

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

    I guess in non coding words I would tell it to remove http://www.examplesite.com/search/ - 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

  5. alchymyth
    Forum Moderator
    Posted 4 years ago #

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

    then you could do a bit of string and array manipulation (assuming that your url structure is always as in your example:
    http://www.examplesite.com/search/state/town
    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];
  6. Mark / t31os
    Moderator
    Posted 4 years ago #

    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..

    $_GET['search']
    $_GET['state']
    $_GET['town']

    ..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>'; ?>.

  7. bremmerm
    Member
    Posted 4 years ago #

    Thanks guys

    t3102_,

    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:

    $city=trim(str_replace('%','',urldecode($_REQUEST['city'])));
    $state=trim(str_replace('%','',urldecode($_REQUEST['state'])));
  8. bremmerm
    Member
    Posted 4 years ago #

    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:

    <title>
    <?php
    
      $shortdesc = myTruncate2(curPageURL(), 46);
    
    $bits = explode('/',curPageURL());
    $bit_nr = count($bits);
    $city = $bits[$bit_nr-1];
    $state = $bits[$bit_nr-2];
    
      if ($shortdesc = "http://www.sportsleaguepay.com/members/leagues") {
    print 'Sports Leagues in '; print_r( $city ); print ',  '; print_r( $state );
      }
    else {
    wp_title('&laquo;', true, 'right');
    bloginfo('name');
    }
    ?>
    </title>

    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:

    http://www.sportsleaguepay.com/

    An example page that looks great is:
    http://www.sportsleaguepay.com/members/leagues/MT/Missoula

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

    THanks!

  9. alchymyth
    Forum Moderator
    Posted 4 years ago #

    try to use == in the if statement:

    if ($shortdesc == "http://www.sportsleaguepay.com/members/leagues")

  10. bremmerm
    Member
    Posted 4 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.