Jump to content

Need Serious Help With a If/Else Statement


Chuck T

Recommended Posts

Hey guys, you'll have to forgive me because i'm an absolute BUM when it comes to php. But i'm trying

to make this work.

 

In my header, i'm running this code:

<meta property="fb:app_id" 
    content="<?php if (get_option('woo_fb_id') <> "") { echo stripslashes(get_option('woo_fb_id'));} ?>"/>

 

You all probably know what it means. I kinda translate it as if the fb_id text input is filled out, get the text. Right?

 

But

 

What I want it do is... check to see if the fb_id filled out here first:

<?php echo get_post_meta($post->ID, appid, true); ?>"/> 

THEN check to see if that text area is filled out and if so, use the text.

 

However i have no idea how to write that!

 

Any help would be GREATLY appreciated guys!

 

Thank you!

 

Link to comment
Share on other sites

I'm kind of confused. If you want to check if the fb_id has a value, like you did in your first snippet, than wrap the second snippet with that if statement you use in the first. I think I may be missing something here. Can you perhaps explain a little better.

 

Also, you can use the empty() function rather than checking of the fb_id is not equal to an empty string. For example

 

if (!empty(get_option('woo_fb_id'))) { ... }

Link to comment
Share on other sites

Hey Mike!

 

Thanks for the quick response. I was expecting to confuse you, that's what i'm here for. :)

 

I want the code the check to see if

 

get_post_meta($post->ID, appid

 

has a value first. If it doesn't, then I want it to carry on as usual

by using the value in fb_id

 

<?php if (get_option('woo_fb_id') <> "") { echo stripslashes(get_option('woo_fb_id'));} ?>

 

So kinda like -

 

IF

<?php echo get_post_meta($post->ID, appid, true); ?>"/> 

 

 

ELSE

(get_option('woo_fb_id') <> "") { echo stripslashes(get_option('woo_fb_id'));

 

If that makes any sense :)

 

Thanks for your help man!

Link to comment
Share on other sites

Oh I see. well if I am interpreting you correctly, you want to echo the get_post_meta($post->ID, appid, true) string if the get_post_meta($post->ID, appid, true) string has a value (IE is not empty), otherwise you want to echo the stripslashes(get_option('woo_fb_id')) string (assuming that the get_option('woo_fb_id') string is not an empty string). Is this interpretation correct?

 

if so you could do something like

 

if (!empty(get_post_meta($post->ID, appid, true)){
    echo get_post_meta($post->ID, appid, true);
}
else if (!empty(get_option('woo_fb_id')) {
    echo stripslashes(get_option('woo_fb_id'));
}

 

Hope that helps

Link to comment
Share on other sites

Thanks for the help Mike.

 

I tried it and it's giving me a Fatal error: Can't use function return value in write context.

 

Here's the code i'm using:

 

<meta property="fb:app_id" 
    content="<?php if (!empty(get_post_meta($post->ID, appid, true)){
    echo get_post_meta($post->ID, appid, true);
}
else if (!empty(get_option('woo_fb_id')) {
    	echo stripslashes(get_option('woo_fb_id'));
} ?>"/>

 

I could be doing something complete wrong. :)

 

Link to comment
Share on other sites

It's on line 22, which would be this one:

content="<?php if (!empty(get_post_meta($post->ID, appid, true)){

 

get_post_meta is a custom field, if you're familiar with wordpress.

appid1.png

 

So it should be checking to see if the post has an appid, if so then use the value.

Otherwise, use the value from fb_id.

Link to comment
Share on other sites

I see. The script must have failed before it got to that syntax error. If you echo out get_post_meta what does it echo? I was under the impression that it returned a string that you wanted to verify, but that doesn't seem to be the case. Sorry, but i'm not very familiar with wordpress, so I'm not really sure what a custom field is. Is there any way you could give more details about this function?

Link to comment
Share on other sites

Ok. well that function, based on the parameters you are passing, should return a single string containing some key value. The key parameter you passed doesn't have any quotes around it. Perhaps it should be

get_post_meta($post->ID, 'appid', true)

This is assuming you aren't using a global constant named appid to store the value you wanted to pass into that function.

 

However, theoretically, assuming you dont have a global constant named appid, it should still work (although it would throw a warning if you have error reporting turned on). if that doesn't work, you could try simply comparing the return value to an empty string, like you did in the original post, IE

if (get_post_meta($post->ID, 'appid', true) != ""){
...

 

 

Hope this helps

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.