This is the second 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 second part we write the function that removes the sidebars from the appropriate posts using the thesis_show_sidebars filter.
Step 2 is now to create a function that removes the sidebars from the post and we’re going to be using the Thesis filter, Thesis show sidebars. We’re going to test if it’s single post and then if it’s a certain kind of post meta then we’ll get the post meta, we’ll test to see what post meta it is and then if it’s the right kind of post meta. Then we’ll return false meta which is don’t show any sidebars.
So that is over here. We are in our custom functions php file for this site. And I have this function that I’ve partially written already which is byob remove post sidebars. And so, the first thing we do is we grab the global variable post so that we can get the post meta and the post id. And then we ask the question, if it’s a single then let’s get the post id… I mean, I’m sorry. Let’s get the post meta and put it in this variable, byob post template style and post id is the current post id. This value here is supposed to hold the name of the custom post meta and that name we get back from the site. Remember, it was post style selection so we’ll just copy that actually and place that here. Post style selection… so get post meta for the current post and the post meta we want to get is the post style selection and true here means we want it stored as a piece of string information. So post this variable, byob post template style is going to be equal to the value of post style selection. And then we say if byob post template style equals and now we have to get those template styles that we named, right?
So for example, if we go back over to this and we go back to the plugin for a second, Verve Meta Boxes, and so we have default, no sidebars, split sidebars and minimalist. So we’ll grab no sidebars first and come back over because this has to be exactly the same. So if the post template style equals no sidebars then return false which means we won’t display the sidebars. And if it’s equal to split sidebars, copy… if it’s equal to split sidebars there, return false. Else, if it’s equal to minimalist, I’m going to go copy that but if it’s equals to minimalist, return false. Otherwise, return true. So default and anything that’s not specified is going to get… the sidebars will still be in place. But for these 3 conditions, no sidebars, split sidbars and minimalist, the sidebars will be removed.
Let’s come back over here and just grab minimalist to make sure that we… you all know often, I misspell stuff. So this is why I copy it. Okay so then if it’s equal to minimalist then return false. Otherwise, return true and then we have this filter, Thesis show sidebars. If you return true to Thesis show sidebars then the sidebars will show. If you return false to Thesis show sidebars, the sidebars will be removed. And then of course, this the name of our function, byob remove post sidebars.
Okay so then let’s go ahead and save that. Actually, I’m going… yeah, okay. That’s fine. We’ll save that then we’ll upload it. And so, we’re going to Larraine.byobtutorial.com, wp content, themes, thesis 181, custom, custom functions php. We’ll upload that and open link in new tab… okay, let’s see… okay cannot redeclare split sidebars, previously declared in home to… split sidebars 231. What did I do there? Oh, it’s actually 221. Byob split sidebars…let’s see, we’ll get back to that. We’ll just say…we can’t say that. You can’t have names that conflict with each other like that. So if I’ve named this twice, I got here once and I probably have… oh, there it is. I have it again there. And so, that was the problem. I need to change the name of this function.
So we’ll just refresh this and the error should go away. Okay and then if we go over to Cherries and look at Sweet Rainiers, I don’t, any longer… or I no longer have a sidebar there. And in this case, the content is expanding to fill the entire space. Sometimes it doesn’t happen and we’re going to add the code to it to make it automatically happen on this case. But if we go back to Cherries and we look at a different cherry say, Bing Cherry, that has the default sidebar automatically because we haven’t given it a specific sidebar style yet.
If on Bing Cherry, we edit the post and we come down here and we select… let’s select split sidebars for that one and hit update and then view the post, the sidebars will be gone again because we told it to turn off sidebars, in that case. And then we’ll do the same thing for… let’s see, we’ll close those. We’ll do the same thing for the 3rd one down, Van Cherries and Van Cherries, we’re going to give it a minimalist and we’ll update. And then we’ll view that post and that will also have an expanded field of space, okay?