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: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 useISSET($id)
— to be safe — before callingget_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/’:
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: