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.
OK. Enough WordPress appreciation and on to the reason for the post.
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] http://artofsimplicity.co.uk/
[sub-site 1] http://artofsimplicity.co.uk/sub-site-1/home/
[sub-site 2] http://artofsimplicity.co.uk/sub-site-2/home/
The way I went about doing this was to retrieve the slug for the parent page, which would either return ‘sub-site-1′ or ‘sub-site-2′.
Achieving this was quite straightforward with the following snippet of PHP code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?php // retrieve the page slug. function get_post_slug($post_parent = false){ global $post; // WordPress global $post if($post_parent){ $parent = get_post($post_parent, OBJECT); // get the post data. $slug = $parent->post_name; // get the post_name/slug }else{ $slug = $post->post_name; // if not a post_parent the post_name/slug for the current page will already be accessible. }; return $slug; }; // same function as above but wrapped up in a new function for convenience. function get_post_parent_slug(){ global $post; // WordPress global $post return get_post_slug($post->post_parent); // once the current post data is loaded you can retrieve the post_parent ID to retrieve that posts post_name/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.

3 Comments
Thanks for this! I used it in a little different application than you did, but it still worked perfectly.
I have a js image slider on the front page of a client site and static images on the rest of the pages. Some of the other pages are children to others, but I want them to have the same header image as the parent pages. So I did this (after including your function in my template functions.php)
Thanks again!!!
for whatever reason, I don’t know how to make code show up right in comments. I put it between <code> or <pre> and it never works out, oh well. Sent you a contact with the code so maybe you could post it and fix it in my comment. :)
Thanks for the feedback Don. I’ve fixed the comment. Need a ‘pre’ tag with lang=”php”. I’m using the wp_syntax plugin.