Add Parent Page Slug and Parent Template to WordPress Body Class

Add CSS body classes for the parent page on all child pages and the parent page template on of a WordPress site with this body_class filter. Ever need to style all child pages of a parent page in the same way or have you wanted to access every child page of a parent page via css selectors for styling? What about selecting all pages that are descendants of a page which is using a specific template?

body_classes_htmlBuilding large websites gets complicated, even in WordPress. Large sites usually mean there are many subpages and sections to the website that may need to be styled similarly. I’ve found it helpful to add a page’s parent page slug to the body class to allow me to alter or target the page or group of pages via css. By default the themes I’ve used have been generous in adding classes to the html body element for easy css selection rules. Things like the post slug, page template, logged in status, page vs post (or custom post type), post id, author… you get the idea. While half the time I don’t need half of this and the other half the time I find myself needing more.

Place this code into your functions.php file and your html body element will have a couple additional classes if they apply. It will have a class delineating the slug for the parent page on all child pages as well as a class delineating the template used by the parent page. This lets me apply styles to a whole sibling-section of a site pretty easily by just targeting the parent-slug on the body. Also adding the template of the parent in case I needed to use that.

post_parent_classesWalking through the code here we’re filtering the body_class function is how we are able to add this. We name our own function and give it a $classes parameter. Then throughout our function we can add classes to this $classes array and they will be output with the rest of the body classes. We need to hook into WordPress at the body_class function with add_filter and specify the hook and specify our own function to be called. In this case we grab the page properties of post_parent and the template of that parent. First set the post variable to reference the global scope, and then check to see if the post is a page with is_page. Then if the post object has a value for the parent (post_parent) we add the parent’s name to the classes array. Then we get the _wp_page_template meta data from the parent to find the template it’s using (if there is no template specified, then it returns default). This is added to our classes if it exists and then we return the classes array to the original body_class WP core function.

[cc lang=”php”]
/////////////////////////////////////////////////////////////////////////////////
// Body class adding page-parent
//
function cc_body_class( $classes ) {
global $post;
if ( is_page() ) {
// Has parent / is sub-page
if ( $post->post_parent ) {
# Parent post name/slug
$parent = get_post( $post->post_parent );
$classes[] = ‘parent-slug-‘.$parent->post_name;
// Parent template name
$parent_template = get_post_meta( $parent->ID, ‘_wp_page_template’, true);
if ( !empty($parent_template) )
$classes[] = ‘parent-template-‘.sanitize_html_class( str_replace( ‘.’, ‘-‘, $parent_template ), ” );
}
}
return $classes;
}
add_filter( ‘body_class’, ‘cc_body_class’ );
[/cc]

There are many more classes we can add to the body_class and like I said, sometimes you need more than what’s already provided and sometimes you need nothing. It all depends on the theme you’re using, what it provides and what your specific site and design require. What other classes have you wanted to see here? How have you filtered body_class to fit your site’s needs?

Application Cache Gotchas

A great writeup from Jake Archibald about the downfalls and downfalls to Application Cache. Having just attempted to building a mobile site using application cache, I remember hitting almost all of these gotchas and realizing that Application Cache wasn't all it made itself to seem. I resorted to actually making a mobile app rather than deal with the manifest file and it's counter-intuitive caching. This article would have helped immensely and I can't tell you how entertaining it is to read anything by Jake.

Embedded Link

A List Apart: Articles: Application Cache is a Douchebag
Good morning! Over in “castle Lanyrd” we recently launched our mobile site, which caches data on events you're attending for viewing offline. I've boiled the offline bits down to a simple demo…

Boston Globe Responsive Process Interview w Scott Jehl

Some good insight into the thought process behind the "famous" responsive web (re)design for the Boston Globe. I think the discussions about the fallbacks and technical challenges very important. Sites should be optimized to have a low overhead and build with the worst oldest devices in mind as well as the latest and best.

Embedded Link

Scott Jehl on the responsive Boston Globe site | Interview | .net magazine
Opera’s Bruce Lawson talks to Scott Jehl of The Filament Group about developing the highly-regarded and responsive website for The Boston Globe

TILT your website with CSS

Great bookmarklet to apply a 3D tilt effect to any site. Reminds me a bit of the upcoming firefox inspector 3d view how it shows the DOM structure with nested items at a higher depth. I hope no one ends up using something similar to make the page rock like the ocean: I can't imagine trying to read while getting seasick. Maybe it would be applicable for a game or something…

Embedded Link

CSS Tilt
We're not quite at the stage where we can fly through the Internet in 3D à la Johnny Mnemonic, diving between skyscrapers of data and a

WordPress Slider Plugin that's Responsive

It seems that every site lately has some sort of slider in it. I'm interested in this plugin that will get the redundant work and even be responsive from the guy(s) at DevPress. I hope to be trying this out on my next project.

Embedded Link

Plugin Release: Responsive Slider | Professional WordPress Themes | DevPress
There is a new DevPress product, and this time it's not a theme. It's a plugin! We just released Responsive Slider. Responsive Slider plugin. Many of you have asked for a theme with slider, an…

Mobile Specific Sites Hurt

I've said it before I don't want a different experience on mobile vs desktop. I always am scrolling around a mobile version of a site to find the link to get to the full site because whoever designed it deemed that I as a mobile user wouldn't appreciate the functionality the site brought. I still agree with the responses to Jakob Nielson's recent publication that we should have mobile versions of our sites. I agree whole-heartedly with the responses from smashing magazine here and Josh Clark and many others. It's encouraging to see the flood of people coming out to contradict the uncharacteristically unwise council from this UX giant. Here Bruce Lawsen gives a great recap of the whole story and the points argued by both sides.

Embedded Link

Why We Shouldn't Make Separate Mobile Websites | Smashing Magazine
There has been a long-running war going on over the mobile Web: it can be summarized with the following question: “Is there a mobile Web?” That is, is the mobile device so fundamentally…

Some Useful Page Load Time Charts

Google analytics shares some data between mobile and desktop speed and segmented by region and industry . I see that (obviously) desktop is a few seconds faster overall.

Embedded Link

Global Site Speed Overview: How Fast Are Websites Around The World? – Analytics Blog
The first step in optimizing any process is to establish and obtain an accurate set of measurement data. In the context of optimizing the user experience on the web, it means that we need to measure t…

Why Decouple WordPress Functionality from your Theme in a Plugin?

So this is all about writing CPTs into a plugin and not in your theme. Is there a time to not do that and include it into the theme? I'm thinking specifically when you are making the custom theme for the client rather than for stock. The client won't really be changing themes willy nilly, since they paid me to do it. I think separating the code sometimes can make it more complicated. It's not like if they change themes and notice some content is gone they can't change back, since the content isn't actually lost, but no longer supported by other themes. If they are my client and I do maintenance on the site or update the theme, I'd be sure to keep the CPT code in any new theme that is developed. And I'd expect that if someone else was doing the same they'd know where to look and how to continue support for the CPTs. I guess it comes down to the expectation that clients don't really need to be changing the theme. If they knew what they were doing changing themes, then I think they should understand that some functionality may be tied to the theme. Or maybe that's the point. Functionality traditionally has been in themes, and we should be moving away from that?

Personally I think it's easier to tell a client to come to me or at least ask if they want to change the theme (not that I'm locking into being my customer) that it would be for them to figure out some message saying the site requires a theme and a plugin in tandem, while it will still kinda work with one and not the other, it will be incomplete either way. Are we expecting them to be able to change themes, and update to new one to display the CPTs properly?

Embedded Link

Theme-ready Custom Post Types in WordPress – Jumping Duck Media
Good advice on decoupling WordPress Custom Post Type implementation from your themes. How to notify a user that some functionality of the theme might depend on a plugin that defines the CPTs. How to override theme template selection from a plugin.
Theme-ready Custom Post Types in WordPress – Jumping Duck Media
Original Article: Theme-ready Custom Post Types in WordPress – Jumping Duck Media
Dougal Campbell's geek ramblings – WordPress, web development, and world domination.

Josh Clark responds to Jakob Nielson's Mobile Stance

Nielson recently stated that he thinks we should keep building a mobile version of a site that is trimmed down and optimized for mobile, and a full version. This contradicts the growing momentum in the industry regarding among other things Responsive Web Design. Josh Clark, another expert in Mobile and Usability, correctly dissects Nielson's stance and explains why he's seeing things backwards in this article at Net Mag.

Embedded Link

Nielsen is wrong on mobile | Opinion | .net magazine
Designer, developer and mobile maven Josh Clark tells us that rather than stripping down, we should be asking how we can do more with the mobile experience

Promising Core WP Theme from studionashvegas

Check out the parent theme on github and use what works for you. I'm very interested to see the functionality Mitch has included in here. It is blazing fast very responsive and looks like a great place to start a new site. Check a demo: http://dev.studionashvegas.com/coretheme/

Embedded Link

I’m Open Sourcing My Core Theme
The last few posts I’ve made on here were diving into the introductions of a Responsive Design (and I plan on finishing