简体   繁体   中英

Get URL As Shown In Address Bar

I'm using some URL rewriting, on top of the URL rewriting that Wordpress does natively.

The basic idea is that I use a category page with an address that appears like it belongs somewhere else, so /blog/type/kids is the true category in WP. I rewrite this with a .htaccess file in /kids/ to make the category look like its actually a page called /kids/programs with this code in the kids folder:

RewriteEngine on
RewriteRule ^programs$ /blog/type/kids/ [P]

This happens in two different locations in the site, but the both load the same WP page behind the scenes. This all works fine.

What I need to do now, is set a variable based on which location it is being loaded from, to ensure that the navigation highlights the proper section of the site, and shows the proper subnav. The problem is that I can't access the new, rewritten URL that the user sees.

Ideally, I'm looking for something like this:

if(strpos($_SERVER['PHP_SELF'],'kids//programs')) {
    $top_nav_item_id = 'kids';
} else {
    $top_nav_item_id = 'programs';
    $subnav_item_id = 'kids';
}

PHP_SELF resolves to /blog/index.php and REQUEST_URI shows /blog/type/kids. So neither are showing me the /kids/programs location that is truly being displayed.

Any ideas how to get this? Maybe WP has a built in tag for this?

Cheers!

I'm also using Rewrite and this is working for me like a charm:

$_SERVER['REDIRECT_SCRIPT_URL']

you can also use

$_SERVER['REDIRECT_SCRIPT_URI']

The WordPress get_permalink() function is fully described at http://codex.wordpress.org/Function_Reference/get_permalink . Here is the usage:

<?php echo get_permalink( $id ); ?>

Since you are operating outside the loop, as you mentioned, you cannot just call get_permalink() , you have to pass the $id to the function. You could set the value of $id from inside the loop, and then use ISSET($id) — to be safe — before calling get_permalink($id) .

If there are any issues after trying that with get_permalink (I was able to test it outside the loop, but I don't have a setup with your mod_rewrite rules), you could use still set $id from inside the loop and then manually construct the URL with the category using something like this, concatenated after 'http://yourservername/':

<?php if(ISSET($specpostid)) {
  $catarray = get_the_category($id);
  echo $catarray[0]->cat_name; } ?>

Try this

$protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http'; $url = $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

The url variable $url will give you the url as shown in the address bar of the browser.

Returns the current url:

<?php 
   function curPageURL() {
     $pageURL = 'http';
     if (@$_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
     $pageURL .= "://";
     if ($_SERVER["SERVER_PORT"] != "80") {
      $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
     } else {
      $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
     }
     return $pageURL;
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM