Wordpress Themes - WP Forum at BFA

Wordpress Themes - WP Forum at BFA (http://forum.bytesforall.com/index.php)
-   Atahualpa 3 Wordpress theme (http://forum.bytesforall.com/forumdisplay.php?f=2)
-   -   Custom Field: Default Values (http://forum.bytesforall.com/showthread.php?t=3100)

gungo Aug 22, 2009 06:39 AM

Custom Field: Default Values
 
I'm using a custom field to position a standard position image in my posts.

Not really knowing what I'm doing, I found an example somewhere and tried this:
custom field - image

I put this in the kicker area of the page/posts:
<img src="/wp-content/uploads/%meta('image')%" alt="%post-title%" />

This works fine as long as there is an image reference in the image field.
However, if the field is blank, I get an error plus an empty image box.

I can make this work by placing a blank .gif in the field every time I post something, but this will drive me crazy eventually.

Is there some php code that would insert the image if there is a value in the field, but leave the area blank (without error and image box) if there is no custom image field - or if the image field is blank?

If conditional code is not possible, would I be able to create a the field with a default value the same way that the meta fields are created when a new post is saved (I couldn't find where this was done).

I want to avoid anything that would be wiped out with theme upgrades.

Thanks!

grasshopper Aug 23, 2009 01:02 PM

Hi gungo -

I too want a conditional call to display my custom fields (because they are taking over my posts when they are empty). I found a great tutorial at another site on Custom Fields and various conditional codes. The tutorial is at perishablepress.com

This was helpful, but my question is in what php document do I put this in? And, once I do, where do I format the output? Hopefully someone can help.

thanks

Flynn Aug 23, 2009 04:00 PM

Try replacing in functions/postinfo.php towards the top

// Callback function for post meta replacement
function meta_values($matches) {
$meta_key = $matches[1];
// "get_post_custom_values" returns an array. Turn it into a string, separated by commata
$meta_values = implode(", ", get_post_custom_values($meta_key));
return $meta_values;
}

with

// Callback function for post meta replacement
function meta_value($matches) {
global $post;
return get_post_meta($post->ID, $matches[1], true);
}

grasshopper Aug 24, 2009 09:45 AM

thank you Flynn -

Gungo I hope this conversation is helping you...and i'm not hijacking your thread.

Flynn -

I entered the code as suggested and when I stripped out the codes in the Byline portion of the Post Info APO options, nothing came up.

I am using the custom fields to provide course schedule information. Please see the link as an example. http://www.grasshoppercloud.com/?page_id=15

I coded all of that scheduling detail into the APO options (post info items). But, some classes have 8 sessions, whereas others have only 1. So, I am trying to only show the session line if there actually is a session.

I looked at using get_post_custom, but I'm such a noob that I'm not sure what I'm doing, where to put the code and then where to format it as a table.

thanks for any help.

grasshopper Aug 24, 2009 12:39 PM

Hi Flynn -

I figured part of it out.

I changed your above code to an "if" code as follows:

function meta_values($matches) {
global $post;
if (get_post_meta($post->ID, $matches[1], true)) :
echo "$matches[1]" . get_post_meta($post->ID, $matches[1], true);
endif;
}

Do you see if this will screw up anything else?

Now, I need to do two things:

(1) actually format the keys and data into a table and
(2) replace the custom "keys" with more user friendly Descriptors.

Can you help me with that?

thanks,

T

Flynn Aug 27, 2009 11:36 AM

1) You can use HTML in the post info item text areas
2) I didn't understand this one

grasshopper Aug 30, 2009 02:55 PM

Hi Flynn -

Thanks for getting back to me. I think there is some problem (inconsistency) between the conditional call for custom meta values and when they are "printed"

I added the conditional call to only get the values if they exist as follows:

function meta_values($matches) {
global $post;
if (get_post_meta($post->ID, $matches[1], true)) :
echo "$matches[1]" . get_post_meta($post->ID, $matches[1], true);
endif;

This code does bring back the values into the top of the post byline area.

But, I created tables in the Post Info Item APO options to organize those values. These are now not being filled in. A sample of the HTML I am using is as follows:

<table class = "organization">
<tr>
<td width="15%">ORGANIZATION: </td>
<td width="35%"> %meta('organization')% </td>
<td width="20%">FEE:</td>
<td width="30%"> %meta('fee')% </td>
</tr>


Is the problem that the conditional values aren't being called into the tables because I am using $matches[1] in the conditional code and %meta% format in the tables? or in the print or preg_replace_callback function?

Thanks so much, I really appreciate any help.

Flynn Aug 31, 2009 10:31 AM

Try with

if (get_post_meta($post->ID, $matches[1], true) != '')

grasshopper Aug 31, 2009 12:25 PM

Thank you Flynn -

I changed that line, but nothing happened. the custom fields are still showing up in the top of the byline area and not in the tables.

Any other suggestions?

Thanks so much.

gungo Aug 31, 2009 07:04 PM

Thanks for the suggestions... will report back when I try them out...

Flynn Sep 1, 2009 04:28 AM

Quote:

Originally Posted by grasshopper (Post 13810)
Thank you Flynn -

I changed that line, but nothing happened. the custom fields are still showing up in the top of the byline area and not in the tables.

Any other suggestions?

Thanks so much.

So you're trying to display them twice? It's possible that they only work once per page. Try using them only once per page. (And I guess you closed the table with </table>, and did just not include that in your post here)

grasshopper Sep 11, 2009 10:37 AM

Flynn -

A million apologies for this very late reply. I normally get those email notifications, but must have missed this one.

I'm hoping you can still help me.

I only want to display the custom fields once in the tables I created within the post info items in the byline area. And, I only want them to show up when there is an actual value assigned, so I did the conditional code in the php document. The conditional calls are showing up outside of the tables.

How do I get the conditional "calls" for the custom fields to populate the tables that I created in the post/info items ATO options?

oh and yes, I am finishing with the </table>, I just sent you a small snippet.

Thanks so much Flynn.

Flynn Sep 11, 2009 06:38 PM

Are you using Atahualpa 3.4.2, the most current version?

Try using this code right in the post info item text area (instead of editing functions/postinfo.php)

PHP Code:

<table class = "organization">
<tr>
<td width="15%">ORGANIZATION: </td>
<td width="35%"> <?php $post_id $post->ID
$meta_value get_post_meta($post_id'organization'TRUE); 
if (
$meta_value != '') echo $meta_value?> </td>
<td width="20%">FEE:</td>
<td width="30%"> <?php $meta_value get_post_meta($post_id'fee'TRUE); 
if (
$meta_value != '') echo $meta_value?> </td>
</tr>
</table>



grasshopper Sep 12, 2009 08:36 AM

Thank you Flynn -

Yes, I am using atahualpa 3.4.2.

I reverted the functions/postinfo.php to its original version and inserted the suggested code into the post info item text area. it's clearly having some affect because, the organization field is coming back blank. I'm hoping there is a tweak, so that it actually brings back the value. could there be some conflict with the meta_values callback function in the .php file? I tried commenting it out, but then get a preg_replace error.

thanks so much Flynn for all of your help.


All times are GMT -6. The time now is 03:16 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.