In this session we show how to add the date at the bottom of a single post in Thesis with PHP code. We use a conditional statement to exclude pages and apply the date only to single posts. Here we created the code to be used and explain how the code works.
Rick: So John, I’ve just unmuted your microphone. Good morning, John.
John: Good morning, Rick. I appreciate your… taking a few minutes of your…
Rick: No problem.
John: You need more clarification or do you just want to go to the answer?
Rick: Why don’t you just describe the question?
John: My client has a blog and a site all rolled together and she wants to center the titles over her articles which are done on pages and keep the articles flushed left on posts. And we’ve already moved the dates published you know, date on to the bottom of the posts and so that’s also showing up on the pages. And I’d like to get rid of that on the pages. So 2 adjustments instead of pages only. I don’t need to mess with the posts at all. Does that make sense?
Rick: Okay well, yeah. The chances are that the code you used to move the date to the bottom… I mean, you used code, right? That’s not… you didn’t find a Thesis setting for that at some place?
John: No, I used a PHP code.
Rick: Well, so a PHP code you used for that needs to exclude pages. So right now, what you’ve done is… well, I don’t exactly know what you’ve done but what you would use is a conditional like if is single. And if you use the conditional if is single to condition the movement of the date then it would not apply itself to pages. It would only apply to single posts.
John: Okay, if I type in the code I used to do the thing and send it to you…
Rick: Well, why don’t you show it to me here? Oh, I see, okay. So you’ve got… yeah, okay. So that is your… so what you’ve placed here is not your actual function but what you’ve placed is HTML. Let’s just look at your… I’m going to change you… make you presenter. Let’s just actually look at your code because…
John: Okay, I did put them in the Thesis Open Hook plugin.
Rick: Well, okay. You’re not going to be able to use Open Hook for that.
John: That’s what I did. I know I will not.
Rick: You won’t because Open Hook doesn’t give you the ability… well okay, actually, that’s not necessarily the case. Let’s just look at your… I mean, what you… Open Hook doesn’t distinguish between… doesn’t have conditionals in it. And so you need something with a conditional in order to make it work.
John: That makes sense.
Rick: And so what you would do there is… after post. What you would do is… I’m just looking at this. Okay so above that P, you would you know, start with a PHP tag, an opening PHP tag. Is that right? Is that the way Open Hook works? You have to open your function or the PHP tag? I suppose it does.
John: I can’t recall. I could always write it in the PHP file.
Rick: Well, just go ahead do it right here and just before that, do the opening PHP tag which is the you know…
John: Question mark?
Rick: No, it’s the bracket question mark PHP and then… no, question PHP.
John: Okay and when you say bracket, which one?
Rick: It’s the less than question mark…
Rick: No… okay so less than question mark PHP.
John: Got it.
Rick: And then give yourself some space and then you would say if is single…no.
John: Is this…
Rick: Actually, you don’t want the closing… that little closing there, that is… it doesn’t work that way so…
Rick: Okay, I’m going to show you what the code would look like here because this is…
John: Okay, that’s fine.
Rick: Let’s see, assign to me. So the code would look like… let’s see, I’m going to change the presenter, make me the presenter, show my screen. The code would look something like this, let’s see dummy project, custom functions PHP. Okay so the code would look something like this. If is_single, that is a conditional tag.
John: I got it.
Rick: In this condition of this tag then you would do stuff. And the stuff you’re going to do, in this case, is you’re going to close out PHP and then we’ll paste your code and then you’ll return the PHP. So let’s see, p class headline meta abbreviation… and then we are missing a closing p tag in this link. Oh actually, this only works if you break it in the right spot. Okay so p class headline meta so here’s your HTML and this is your… the function. So if is single, do this. Now what you have to add to the code for Open Hook is you have to have an opening PHP tag and the closing PHP tag.
Right now, this is in a PHP file so it doesn’t actually need an opening tag. But to add an opening tag, it is the <? PHP and then the closing tag is ?>. And so what you have here is you’ve got the opening PHP tag if is single then a closing PHP tag then your HTML then an opening PHP tag, the bracket of your if is single and then the closing PHP tag. So that’s what the code would look like. Essentially, this is the conditional code for specifying that this only happens on single posts. It doesn’t happen on anywhere else. And that’s not something Open Hook does by itself. Open Hook doesn’t have a mechanism by itself for… let’s see, so I’m going to put this in the chat window just so that you can…
John: Okay, well I took a screenshot of it. That one would be great.
Rick: Okay. But Open Hook doesn’t have a mechanism for having these conditional tags in them but that’s what you would need in order to make that conditional so it would only exist in the one case. Okay so that’s the first…
John: Well, since you’ve done this, I could also take it out of Open Hook and just paste it in my php file?
Rick: Well, if you were going to convert this to a function and get rid of these tags…
John: Oh then you have to add all that, right. Okay, okay.
Rick: You add this to a function. Now this is absolutely how I would do it. I would not use Open Hook to do this task. However…
John: Yeah, tell me how you would do it. That’s great.
Rick: Function and then create a name so byob add date to post bottom. Okay so there’s our function name and then I’ll just indent it. And then you need an add action and I think you were using thesis… pardon me… in quotation marks, thesis hook after post. I believe that’s what you were using there and then the name of your function. Control Z, control copy and close it out like that. So the non Open Hook way of doing this is giving it a function name and then adding the action. If you can do this, there’s absolutely no reason to use Open Hook. I mean, the only reason to use Open Hook is you can’t do this. But if you can do that then you would just use your custom functions PHP file instead and…
John: No, go ahead.
Rick: So I’ll go ahead and post this on the forum too so you can get access to this in the Live Answer code section of the forum. But that’s how you would add this date to the bottom of posts only. If you wanted to add it to both posts and pages, instead of saying if is single, you would say if is singular. If it’s singular then it applies to both posts and pages. But if it’s single, it applies only to posts and if is page, it applies only to pages.
John: So… okay, I got it. I got it.