In this session we show how to use get_post_meta () with the Custom Fields Plugin using the embed code that the plugin generates. We go through the process of capturing the value of the post meta, putting it in a variable and using that to display something on the page. We also use a conditional statement to limit this action to specific posts, echoing HTML and using the HTML. We show the way to use the PHP to capture the content of the post meta and place it in a PHP variable, writing HTML and using the PHP variable in the HTML. This process happens to every post so we also show how to make it happen only on a specific post by creating an if statement.
Member: And then the other aspect’s I don’t know whether this is feasible. I’ve done a bit of searching on the… generally on the web for it is that one of the custom type fields that I’ve created is for the website address.
Member: And ideally, I want it to be clickable so the… obviously, it becomes a back link to people’s sites because that’s part of the way that we’re selling the directory listing is you can get a back link to your site from us.
Member: And I don’t know where to go or to look. I’ve read all… I’ve got something called Loop Buddy which I think is produced by the same people that the Backing Buddy that you use.
Member: But it looks quite expensive.
Rick: At this part, at this point what I would do is just to use some custom CSS, I mean, some custom php for that because you’ve already done the hard part which is define the field and capture the data. Now the easy part really is recalling that data and putting it on the page.
And so for example you know, what you’re really talking about is the function get post meta. And actually, if you look at the embed code that plugin generates when you click on embed code, it generates a little get post meta field. And let’s see, so this is the code for that community library project, right? And so we have this… we have the code that says you know, you take a variable and then you make that variable equal to get post meta and then the name of that post meta. And so now you would have the URL in the address. I mean, I’m sorry. You would have the URL in this field, in this variable. And then probably the easiest way to do that, the next thing then is to do something like this, echo and then you’re going to use HTML which in this case is a href… you know, I can type… href equals. And then you’re going to put the value of that variable in there so now you need the concactenation symbol so that’s that. And then you’re going to put the variable in. And then you’re going to close out the HTML, the opening HTML tag so again, it’s a href equals and then you’re closing with a double quote and then we won’t put a title in and that kind of stuff. But you could obviously, enter the title and everything else that you would normally do there.
So there’s your opening a tag or opening link tag. And then another concactenation symbol and you’re going to put that variable in again and now this is the link text. And then finally, a third concactenation symbol and you’re going to close out the link tag so /a.
Okay so what we did was… and this is just fake in this case, right? But we’re assuming that this post meta that we’re grabbing is a URL, right? So we’ve got the variable equals that post meta and then we create an HTML, a link that says echo a href. So we’ve got our standard link tag, that href is, of course, the variable. Close out the opening link tag. Again, reinsert the… use the variable again as the link text so now the href and the link text are going to be the same. And then close it out with the closing link tag. And so…
Member: And this effectively… another hook. When I watched your videos, will this… I can see the process here. It’s understanding the… it should be just saying if this equals this then this is what you make happen, is that right?
Rick: Well, that’s right. This is not a hook. This is… the hook is the place in the WordPress process that you want the behavior to happen. The behavior is the function. So you create a function just like this is, function byob catalog items data. In fact, let’s just go ahead and put this together.
So we start off with function and let’s see… we’re just going to call this… it’s very important to use unique names for these files or these things. I’m going to say byob but you can use any kind of a unique identifier for your function name. And then let’s see, insert… what is this, listing meta. Okay so now you have a correctly formed function and then what we want to do is… let’s add the reference to the hook here. So that is an add action and we’re going to say, in this case, thesis hook after post. And then we want to put the function name in here.
Okay so now we have an add action and the add action is to add this function to this hook. Okay now, we’re going to get the information. And so the first thing we need to do is access the global post variable because the post meta is contained inside that post variable. Okay so the post variable has the headline, all the post meta, the post itself and the post author, any tags, categories associated with it. It has everything associated with that post is inside this global post variable. So you want to be able to use that post variable inside this function. So we’re going to say global post and then what we want to do is create that get post meta function. So let’s come back up to the top here. Okay, I’m just going to grab the code.
Okay and so now this is going to be… let’s see, listing URL. So that’s the name of our little function here. I’m sorry, the name of our variable. And we’re going to get the post meta of the post id that we have right now and the post id that’s referenced in this post. And then the name of that post meta, what is the post meta name?
Member: Hang on. I presume I’m going to go to my Custom Press content types and have a look at it there, am I?
Rick: Actually, what I’m going to do is I’m going to make you the presenter and we’re just going to look at your screen together here.
Member: Am I back?
Rick: Yeah so…
Member: So I’m assuming I go into custom fields.
Rick: Sure. Let’s go to custom fields, see what it looks like, what it says there. Okay, website. So yeah, grab the embed code.
Member: There we go.
Rick: Okay so this is… okay would you copy that…
Member: This bottom bit or the php bit?
Rick: Well actually, what’s really interesting about this, I didn’t notice this before. But yeah, you can go ahead and grab the whole thing. That’s fine. Yeah, grab that bottom bit.
Member: The bottom one?
Member: Pop it into the textbox, yeah?
Rick: Yeah, in the chat window there, yeah.
Member: Yeah. This might have changed because it says it has been upgraded. So this might look different than it did you know, last Tuesday afternoon.
Rick: Okay. I think it does look different than it did yesterday afternoon. Send that question so I can get at it. Okay, make myself the presenter, show my screen. Okay, I’m going to post this thing here just to hold its location. This is the actual post meta right there. So it starts with the underscore, _ct text. So we’re going to copy that and instead of call number here, I’m just going to paste that.
Member: And obviously, this is specifically addressing to make the URL clickable.
Member: But the… when I watch the videos, that will help me create the proper action to put all 4 items up into the post.
Rick: Yeah but we’re going to play with that right now anyway.
Rick: So we’ve got our listing URL and we are filling that variable with the value of this post meta. And so the next thing to do then is simply to do our echo and we’re going to say a href equals double quotes and then space concactenate space. And then we’re just going to put the variable in there and then we’ll say we’re going to concactenate again. And so then our double quote, that ends out the href. And we may as well say title equals visit the website. Okay so there’s our title closing of the opening a tag and now we need another concactenation and we’re going to use that listing URL again. And now that’s going to be the link text and then finally close out the link with a closing a tag and semi-colon.
Okay, what this is going to do is take that captured post meta and create a link that has the listing URL as both the link itself and the link text or as both the link address and the link text, right? And it’s going to take that Thesis hook after post and insert the listing meta.
Now let’s look at one more example of this on your site here. So I’m going to come back and make you presenter again.
Member: Okay. Let’s look at one of these three here.
Rick: Yeah, let’s look at the contact… yeah, let’s get the contact name one.
Member: Yeah, I’m assuming it’s this here.
Rick: Yes, it is. It is that right there, yeah.
Member: So I don’t need…
Rick: Put that in the chat window.
Rick: Okay make me the presenter again. Okay and so let’s see, let’s paste that down there or write it. Let’s just say… yeah, listing name and it’s going to be get that post meta there. So now we’ve captured listing name and the listing URL.
And now what probably makes the most sense is for you to make a little block to essentially wrap this stuff in div. So the first thing to do, I think, in that case then is echo div class equals listing meta block. Okay this gives you a handle that you can use to style this stuff, okay? And naturally, it needs a closing div. Okay so you make an opening div and your closing div and now the next thing you’re going to do then is put the name in. Now do you want to have anything in front of the name like name or do you want it just to have the name in there?
Member: At the moment, I’ve just had it as contact name then contact number and email. At least it makes sense so I think contact name assuming that it won’t put the field… I’m assuming it’s not going to put the field name and you don’t have to tell it to that, is that right?
Rick: That’s right. So we’ll say p and then contact name colon space space and we’ll just close that. And then echo and then in this case, it’s going to be… actually, rather than closing that, let’s go ahead and concactenate it in. So then we put in the listing name and then we may as well close out the p tag. So we concactenate in one more /p. Those need to be contained in single quotes. Okay so now you’ve got your contact name with listing name and a p.
Member: And then I just repeat that other two fields.
Rick: And then you would do the same thing here. What I would do is just copy this and then say, instead of contact name, I’d say website. And then you’ve got your a href and then we just want to close out the p tag here again.
Now you could also you know, do this as an unordered list instead of being in paragraphs. So this could be a you know, there could be a UL and then this should be an LI if you wanted them as that instead of paragraph tags. But in any case, what you have here is you have this process of capturing the value of that post meta, putting it in a variable, echoing some HTML and using the variable. So you could here say ‘visit the website of…’ and then close that out for a second. Oh, we probably want a space there and then again, put the listing name in. So now instead of ‘visit the website’, it’ll say ‘visit the website of this listing name’ as the title.
Member: Will the listing name be the person’s actual name rather than the name of the…
Rick: Whatever you put here. Whatever that is.
Rick: It could be the person’s name. It could be the business name. The thing I’m trying to show you here is the way in which to use these variables, the way of sort of intersperse php variables inside of HTML. So what we have is a combination… we’re using php to grab the.. to capture the content of the post meta and place it in a php variable. And then we are writing HTML and using that php variable in the midst of the HTML. And that’s… and in the grand scheme of things, this is, I believe, a lot easier to do than to go try and grab another plugina nd make it do this.
Rick: Because the hard part of all of this was actually writing the code to capture this piece of information. Once it’s captured, using it is pretty rudimentary php.
Member: Right. So what’s the easiest way for me to get that from that screen?
Rick: Well, I’m just going to post it on the forum right now.
Rick: And on the forum… by the way, I’ve fixed these videos now, these posts so you can go see that. Let’s see, okay under Live Answer code… Live Question and Answer Code… what’s the name of this plugin again?
Member: Custom Press and Directory. The two combined, isn’t it?
Rick: Yeah. Okay so we’re just going to add a topic that says you know, insert Custom Press post meta in directory listing. Actually you know, there’s one other thing that we need to do here because right now, this is going to happen after every single post. This happens after every post. So what we really want to do first is…
Member: It’s only on the directory listing post, isn’t it?
Rick: It’s only on the directory listing post so we need to make sure that we have the right post type first and that’s right here.
Member: The post type was directory under listing.
Rick: Yeah so once we get our global post, the first thing to do is just to make sure we’re talking about the right… indent that in. Okay so we say if it’s this post type and in this case, the post type is… I’m sorry, directory_listing?
Member: directory_listing, yeah.
Rick: So if it’s directory_listing then do the rest of this stuff. If it isn’t, nothing happens.
Member: Because if you didn’t tell it that then it would go to try and find it. And would that then cause a function error because it didn’t find it?
Rick: No, it wouldn’t but it would still print out contact name, website. It would create a broken link because this stuff would all be written. It’s just that these variables would be blank.
Member: I don’t know if this is going to happen but what would happen if the particular listing didn’t have a website address then? It would just effectively be… it would just say, “Visit the website” or will it be blank because it wouldn’t be entering anything into the box in the first place?
Rick: Well, that’s what happens here. Probably the best behavior would be to have an if statement that says here, if listing URL and this works sometimes. If listing URL then echo all of that. So what this really means is if there’s something inside of the variable listing URL…
Member: Yeah, then do it.
Rick: Right. Now unfortunately you know, this doesn’t really fix… the problem is that if somebody you know, types fish sticks in the place of this listing URL, it’s going to say, yeah, it’s fish sticks. It’s going to put fish sticks here and fish sticks there and it’s not going to work anyway. It’s totally not going to be a functioning link.
Member: At the moment, I think because of the volume of actual directory listings there are going to be and the fact that there’s going to be… rather than using a plugin or something to collect payment, people are probably going to email me the details and I’m going to be adding them myself. So from that point of view, it’s only me entering data in the boxes so I’ll know to carefully enter it.
Rick: So one way to change this up is you could say if listing URL is equal… pardon me… is not equal to blank. Okay so that’s another way of doing this. And actually, I find this is a little bit more reliable than if listing URL. Theoretically, if listing URL is supposed to work but there are times in which that question returns true even if there’s nothing there. I’m not sure that I understand exactly why that’s the case.
So when I do a statement like this, I ask you know, if it’s not blank then do this. So what will happen is… and in fact, the way I would do this is take another… take this another step and I would say if listing URL is not blank and… or which is that double… or listing name is not blank.
Actually, I would do it the other way around. Yes, if listing URL is not blank or if listing name is not blank then do this stuff so we’ll indent it again. So we’re not going to do anything. If these two values have nothing in them then we won’t do anything at all. If one of the values has something in it then we’ll do the div class you know, the div and then we’ll do the contact name. And actually, I would do the test again for contact name before I write out the contact name so that you know, you tested for value before you echoed it so that you always had you know, decent looking output. So you never add contact name blank but what you had is if there’s no contact name, it doesn’t say it. So then that is, of course, if listing name then… so now you know, what you’re doing is you’re just kind of layering complexity on to it. But that’s really what good programming does here.
So the first thing we did was just to figure out what the sort of bare minimum was we want to accomplish and then we just kind of… we gradually added a few little bits to it like you know, wrapping it in a div and then deciding whether or not to print out anything about the contact name if there was no contact name. And then deciding whether or not to pring out anything at all if there is nothing at all. Did I make it more complicated than it needs to be for you to understand that?
Member: No, it’s great. It’s… I mean, it’s something that would have taken me probably weeks to talk about.
Member: I would have persevered and got there but…
Rick: Sure. I mean, it’s just a… I mean, what you want to do here isn’t really particularly complicated programming but it does rely on a bunch of programming knowledge.
Member: Right. And that’s something I lack right now. It took me probably about an hour last night just to work out which was the correct class name in order to put the margin padding under the heading. But I got there.
Member: And at least by using the process of Web Developer and Firebug, it meant that… I wasn’t loading something that didn’t do anything and not… you know, I was just able to sit and play with Web Developer and it was brilliant. And I wouldn’t have known to do that if I hadn’t have started on looking at some of the videos over the last couple of weeks.
Rick: Well, that’s good. I’m glad. Okay so we’re going to come back over here and just post that into this then. So yeah, I’m just going to… let’s see, where did it say? There it is, post new topic. Sometimes this pasting of code doesn’t really behave exactly the way you want it to but… yeah, it looks fine there. So anyway, there’s the code. By all means, copy and paste it and then just use this process you know, to iterate through it, to get it to be exactly the way you want it to be, including maybe looking at a different location for after post, I mean you know, maybe it’s after headline or something. But you just fool around with it until you get it where you want it.
Member: Well, I’m hoping obviously because that’s the bit where when I watch those videos, that starts to make more sense. I understand the process of what the hooks are all about from the discussion we had yesterday. But watching actually what you’ve done is hopefully going to cement it in.