This is the third in a short series on creating custom template styles for posts. We will be using Thesis hooks and filters to create these “post templates”. In this part we write the function that adds a custom body class to each custom template using the thesis_body_classes filter.
So that takes us on to step 2 which is to add the custom body classes so that we can redefine how we want the CSS to work on each of these. For example, if we come back over to this one and Cherries and we look at this Cherries and we view the source, the body class is just custom. That’s all it is. It’s not… it doesn’t have any other type of association with it. And so, what we’re going to do is add something to this custom body class so that we can then define the widths of things with that custom body class.
And so, we’re going to be doing that by using the Thesis filter, Thesis body classes. And again, we’re going to use that same initial conditional. If it’s single, we’re going to get the post meta, we’re going to test the template style and then we’re going to return the correct classes based on which template style we have. And again, I’ve started this function here, byob post body classes.
Now notice how most often you see a function, especially in Thesis with nothing in between these brackets here. Well in this case, we are going to… we are capturing what is the existing class or the existing value of classes for this filter. And so, this filter, when you call this filter, it looks for this variable called classes. And what we’re doing here is when we call this filter, we’re bringing in the current value of classes and then we are going to return the new value of classes.
Now if there is no change to classes then this return classes is going to return what it brought in. If there is a change in this logic then it will return the changed value of classes. So if we didn’t have return classes here, say we put the return classes inside of our if statement if it’s single and put our return classes there with this filter, what would happen is the custom body class would go away entirely for that post because we essentially would be wiping out. Well, using this filter wipes out the current condition and either replaces it with nothing or replaces it with something.
So we’re going to replace it with something here and the something we’re going to replace it with is…well, first we’re going to get the post meta and again, it’s the same post meta so it’s post style selection. We’re going to get that post meta and then if the post template style equals no sidebars then we’re going to return the classes, plus post no sidebars. And if it is the template style split sidebars then we will return the classes post split sidebars.
Now these names, post no sidebars, post split sidebars, these are arbitrary, right? We are… what did I do? Post split sidebars… spelling? What am I spelling wrong? Post split sidebars, post no sidebars. I see, okay.
Again, this is arbitrary in that we will then add this class to our CSS file so it doesn’t have to be perfect here. But nevertheless, it would have to be exactly the same though. If you misspell here, you have to misspell it in your custom CSS file too so might as well spell it right.
And then finally, if it’s the post template style equals minimalist then we will return post minimalist. Okay, minimalist. And so, this will add these, either post no sidebars, post split sidebars or post minimalist to that custom body class if we selected those in that post. So again, it’s add filter Thesis body classes, that’s the filter we’re using and then we’re using this function name here.
So we’ll save it, we’ll upload it and then we will come take a look at it. We’re going to refresh this and now, we’ll view the source. And now, you can see it has custom post no sidebars associated with it when it used to just say custom, right?
And if we go over to Cherries and look at Bing Cherry 2 and view the source, it has custom post split sidebars. And then if we go to Bananas and look at the Bananas 1 and view the source, it stays custom because we haven’t assigned anything special to that post. And then if we go back to Cherries and we look at Van Black Cherries and again, view the source. Here, we have post minimalist added.
So our system is working just fine.