Get the parent slug of a post in WordPress
Beware this content is over 9 years old and considered stale. It may no longer be accurate and/or reflect the understanding of the author but remains here for reference only. Please keep this in mind as you use this content.
I’ve been doing a little more WordPress (v2.9.2) hacking as a new project required a little more CMS-like control over a website than WordPress by default provides.
As a result I’ve resorted to extending WordPress and the results have been pretty encouraging already. I have to say, for the record, that the WordPress team has made extending the WordPress platform a breeze through custom plug-ins and hooks.
I recently needed a quick and easy way to get the slug for a post parent.
The site I’m working on has a series of sub/mini-sites under the root directory, meaning that there will be multiple homepages for each of the sub-sites and each sub-site will have it’s own navigation options.
As a result, I needed a quick way of determining which sub-site a
home slug returned for the current page belonged to. Now, the way I had structured the pages in WordPress was by creating a separate
home page for each sub-site as a page of the root site so the URL’s would be:
[root directory] https://artofsimplicity.co.uk/
The way I went about doing this was to retrieve the slug for the parent page, which would either return
Achieving this was quite straightforward with the following snippet of PHP code:
// Retrieve the page slug.
Maybe this simple little snippet will help those trying to achieve a similar effect in WordPress.
At the time of writing WordPress 3.0 has just been released so we’ll see if it’s no longer needed in new version.