• Resolved Ricky Irvine

    (@dressedinvalue)


    I need to translate the following to PHP:

    if a custom field exists with the key name "quoted" then display this:
    <h2><strong>“</strong>[key-value]<strong>”</strong></h2>
    
    ELSE if a custom field exists with the key name "unquoted" then display this:
    <h2>[key-value]</h2>
    
    if no custom field, don't display anything

    It seems like a simple task, but I can’t wrap my head around custom fields. Can someone help?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter Ricky Irvine

    (@dressedinvalue)

    Sorry guys, I forgot to finish writing the title of this entry, and now it’s too late to edit.

    Ok but what if both exist? Just say by some odd occurance both exist for the post (if they wouldn’t normally) how would you like the code to behave?

    Should one be given priority over the other?

    Should anything be shown if none exist?

    Thread Starter Ricky Irvine

    (@dressedinvalue)

    I didn’t think about that. But it’d be best to use the same as the second IF statement (“unquoted”).

    This is a basic example..

    <?php $cfield1 = get_post_meta( $post->ID, 'field1name', true ); ?>
    <?php $cfield2 = get_post_meta( $post->ID, 'field2name', true ); ?>
    <?php if($cfield1) { ?>
    	<h2><?php echo $cfield1; ?></h2>
    <?php } elseif($cfield2) { ?>
    	<h2><?php echo $cfield2; ?></h2>
    <?php } else { // You can remove this line and the two below if you don't need them - just leave the first "}" (ignore quotes) ?>
    	Something else when both aren't present.
    <?php } ?>

    Change field1name and field2name to match your field names.

    Thread Starter Ricky Irvine

    (@dressedinvalue)

    You are a gentleman and a scholar! A lifesaver! This is perfect. Thanks so much!

    Custom field questions are posted pretty much daily, so it’s usually pretty fresh in my head.

    You’re welcome in any case… ??

    I’ve just installed WordPress with Pixeled theme in my brazilian blog, but I do not know PHP or XML syntax or programing. I′d like to edit files of this theme to change field names exibited on the blog, like “leave a comment” to the expression in brazilian portuguese. How can I do that folks ? Thanks a lot.

    lfsp07 please post your own “new” topic with your question.

    At the bottom of the post list is the “New topic” form for starting your own thread, click here if necessary.

    Can a mod please remove this reply and the one above once the user has a chance to discover the post form? … to keep the thread tidy? ..

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Custom fields: if t’ is closed to new replies.