0

WordPress has a built-in variable $pagename that will give the current page. I am looking for something that will give the full url including the path before the current page. Details below.

I am using a page template to display a folders contents depending on which page someone is on. Right now this method is working for top level paths, but not for anything nested. I understand why it isn't working, but I am not sure how to fix it. Here is an example of what I am doing now:

$dir = "wp-content/uploads/Docs/".$pagename;

So that will give me the correct path to the top level pages, but anything below them will not work. I know the easy solution would just to keep all folders at the base level in the upload directory, but I want my server side files to make sense also. How a url using $pagename looks now:

example.com/accounting

But lets say that I have a page within /accounting like this:

example.com/accounting/accounts-receivable

Using the $pagename method will only provide me with this:

example.com/accounts-receivable

My preferred solution would be to use something like $pagename and get the full path. I have looked in the Codex for a solution, but I am either not seeing it or it is not in there. I am specifically looked through the global variables (http://codex.wordpress.org/Global_Variables) to see if there is one. In that Codex page they do not mention their $pagename variable although it does exist. Hopefully there is a similar variable for the full path.

1 Answer 1

1

You could just get the permalink and strip the home root from it-

$path = substr( get_permalink(), strlen( home_url('/') ) );
1
  • Thank you. Not enough rep for an upvote, but I appreciate it.
    – Willee5586
    Apr 4, 2014 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.