PDA

View Full Version : Wordpress Custom Field Use



orion_joel
10-17-2009, 08:32 PM
Hi,

Just a quick question, regarding if i have used the correct technique here. This being because i know pretty much nothing about PHP, but have used a process of elimination to get to the following, and it appears to work how i want it to but i dont want to leave any holes.

The idea is i want to display a Creative commons Attribution link if the image needs it but display nothing otherwise.

Basically i have set the value $cc_image to check for the custom field cc_image, then using the If...Else statement to display the small piece of code if the customer field exists otherwise display nothing? I think this is possibly pretty basic stuff but to me, maybe not so.


$cc_image = get_post_meta($post->ID, 'cc_image', $single = true);



<?php
if($cc_image !== '') { ?>
<br><img src="CC_image file location" alt="CC">Photo Credit: <?php echo get_post_meta($post->ID, 'Attribution', $single = true) ?>
<?php }
else { echo ''; } ?>

Please any suggestions are appreciated, though i am assuming since it works how i expect it, then it is probably correct. But when you dont know something you can always miss something without really knowing.

vangogh
10-18-2009, 12:31 AM
Looks good. I assume you'll be replacing "CC_image file location" with the real path to the image.

One thing that's not necessary, but you may want to do. By default WordPress uses an XHTML doctype so you my want to use xhtml and use self enclosing tags



<br />
<img src="" alt="" />


Again not necessary, but preferred unless you've changed the doctype in your header.php file.

The custom field code looks good.

orion_joel
10-18-2009, 03:30 AM
Thanks for the reply Vangogh.

Is it just adding the / with a space after before closing the < >? That seems to be the only difference have added.

Again Thank-you for taking a look.

Patrysha
10-18-2009, 03:37 AM
It is so nice to have someplace to come for good WordPress advice - the knowledge base on this forum never ceases to astound me.

vangogh
10-18-2009, 11:59 AM
Joel that's what you do on tags like hr and img that are self closing. On things like paragraphs you have to make sure to use the closing tag. The idea is to always close your tags, some tags will have a close tag and others are self closing. For the self closing tags yes it's just the space and /

Also write all tags in lowercase instead of uppercase.

There are a few other things, but if you do both of the above you've got much of it. In the not too distant future html5 will be coming and we'll all be switching to that. So you'll be able to go back to forgetting to close tags and using uppercase, though it will still be best practice to use lowercase and close tags.


It is so nice to have someplace to come for good WordPress advice

Ask away when you have questions. I work almost exclusively with WordPress now and I think eborg is the same way. I can't remember the last site I built without it.

billbenson
10-18-2009, 05:41 PM
!==

change this to !=

I don't think thats a legal command:

= sets a variable ie $bob=1 the variable $bob is set as 1

== is used as a comparison ie if($bob==1) {echo 'bob is no 1";}

!= is not the same as ie if($bob !=1) {echo 'bob aint no 1';}

I've never seen !==

vangogh
10-18-2009, 09:43 PM
Bill !== is a legal command, though you're right that Joel is fine to use != instead

=== is a comparison to see if two things are identical. If they are equal and of the same type. You can think of it as a more strict version of the == comparison.

So !== is checking to see if to things are not identical. For example you could test to see if 1 == true and 0 == false. Both should evaluate to true. However 1 === true and 0 === false should return false since they aren't identical.

Either is probably fine in Joel's case. I don't think there's any difference in practice when comparing two things you know are strings. You typically don't see the === or !== that often, but they are valid.

orion_joel
10-18-2009, 10:22 PM
The primary reason i used the !== was because i was working the theory though against another statement the checks for the thumbnail.

It works with != so i will just stick to that.

Also i the list of HTML / XHTML tags i found is HTML 4.01 / XHTML 1.0 Reference (http://www.w3schools.com/tags/default.asp) it does actually show a "/" on the self closing tags.

vangogh
10-18-2009, 11:57 PM
Yeah the / is the self closing part. Did I seem like I was questioning that before?

Not every tag self closing so you wouldn't have <p /> for example, but you would want to make sure every <p> tag has a corresponding </p> tag.

billbenson
10-19-2009, 02:50 AM
Your right VG. I did a google search before posting and it didn't show up. I had just never seen it before. Found this on php.net though:

$a == $b Equal TRUE if $a is equal to $b.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4)
$a != $b Not equal TRUE if $a is not equal to $b.
$a <> $b Not equal TRUE if $a is not equal to $b.
$a !== $b Not identical TRUE if $a is not equal to $b, or they are not of the same type. (introduced in PHP 4)
$a < $b Less than TRUE if $a is strictly less than $b.
$a > $b Greater than TRUE if $a is strictly greater than $b.
$a <= $b Less than or equal to TRUE if $a is less than or equal to $b.
$a >= $b Greater than or equal to TRUE if $a is greater than or equal to $b.

vangogh
10-19-2009, 11:30 AM
Yeah. It's really not something you see used often since most of the time the equals operator works well enough. I remembered it being in some of the books I read when learning PHP and then did some searching to make sure it was true for PHP. I didn't find anything through Google either, but did find the page from php.net.

I can't think of a single time I've ever used either === or !== in any script or program. Just knew they existed.

orion_joel
10-20-2009, 09:29 AM
The !== appears to be used at least in the one theme that i am using on the page from elegant themes, when checking for the existence of a thumbnail or not.

for the / in the self closing tags, didn't sound to be questioning, however i was not sure if that was the bit you meant in the first post did the search and found the listing of valid HTML 4.01 / XHTML tags, which is the link i shared before.

vangogh
10-20-2009, 11:23 AM
The choice to use !== or != will come down to what you're comparing and how exact you want to be. Most people will use !=, but !== is the stricter test.

I guess with the self closing tags I thought I was saying the same thing as the page you linked to. So I was confused. Guess I didn't do a good job saying what I meant. As long as you got the information it's no big deal. The important things was just to know about the self enclosing tags.

orion_joel
10-20-2009, 08:09 PM
The thing is i did not know what self closing tags are. But also thought the tag reference may be useful for others that are not so familiar with coding in general.

Anyway i think we are all on the same page now.

vangogh
10-20-2009, 11:39 PM
Oh yeah. I think it was good you posted the link. Like you said others will be reading and it can provide a good reference.

The self closing tags are an xhtml thing. html tags should be closed, but browsers will often display things right if you forget. Some tags like the img tag don't have the closing tag. xhtml is more strict and says you have to close all tags and so those tags that don't have a natural closing tag get the self closing tag.

Of course in a few years we'll all mostly be using html5, which won't need the self closing tags though it will happily accept them.