Forums

[resolved] Custom fields: if t (9 posts)

  1. Ricky
    Member
    Posted 3 years ago #

    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?

  2. Ricky
    Member
    Posted 3 years ago #

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

  3. Mark / t31os
    Moderator
    Posted 3 years ago #

    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?

  4. Ricky
    Member
    Posted 3 years ago #

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

  5. Mark / t31os
    Moderator
    Posted 3 years ago #

    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.

  6. Ricky
    Member
    Posted 3 years ago #

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

  7. Mark / t31os
    Moderator
    Posted 3 years ago #

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

    You're welcome in any case... :)

  8. lfsp07
    Member
    Posted 3 years ago #

    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.

  9. Mark / t31os
    Moderator
    Posted 3 years ago #

    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? ..

Topic Closed

This topic has been closed to new replies.

About this Topic