Custom Fields – Boolean Display of True/False to Yes/No

I am importing fields from a Real Estate site.

The Import of one Custom Field looks like this…

Read More

Field Name after Import —> assistedLiving
Field Value —> false

to output the display in wordpress i use this…

<?php 
    global $wp_query;
    $postid = $wp_query->post->ID;

    if ($immopress_property['assistedLiving']): ?>
        <li class="assistedLiving">
            <span class="attribute"  style="width:200px;display:block;float:left;"><?php echo 'Seniorengerechtes Wohnen' ; ?><span class="wpp_colon">:</span></span>
            <span class="value"><?php echo get_post_meta($post->ID, 'assistedLiving' , true); ?>&nbsp;</span>
        </li>

        <?php wp_reset_query(); ?>
    <?php endif; ?>   

The Result on Site looks so…

**Seniorengerechtes Wohnen:false**

How can i display False/True Values to display Yes/No or in German Ja/NEIN?

The Funktion to display all Field (Display it Right with yes/no):

function immopress_the_fields( $args ) {

    global $post, $ImmoPress;

    extract( wp_parse_args( $args, array (
        'id' => $post->ID,
        'exclude' => $exclude,
        'echo' => TRUE
    ) ), EXTR_SKIP );

    $fields = immopress_get_fields( $args );

    if ( !$fields ) 
        return false;

    $output = '';

    $output .= '<ul class="immopress-fields">';

    foreach ( $fields as $key => $value) {

        $entry = $ImmoPress->values[$value];
        if ( $entry == '') 
            $entry = $value;

        $output .= "<li class='immopress-field-$key'>";
        $output .= "<strong class='immopress-key'>{$ImmoPress->fields[$key]}: </strong>";
        $output .= "<span class='immopress-value'>$entry</span>";
        $output .= "</li>"; 
    }

    $output .= '</ul>';

    if ( $echo ) {
        echo $output;
    } else {
        return $output;
    }

But i need to display only Single Name & Values not all importet fields.

Code for the Shortcode..

function immopress_fields_shortcode( $atts ) {

    $args = wp_parse_args( $atts, array (
        'echo' => FALSE
    ) );

    return immopress_the_fields( $args );       
}
add_shortcode( 'immopress_fields', 'immopress_fields_shortcode' ); 

Hope someone can Help me with that.

Thanks

Tony

Related posts

Leave a Reply

2 comments

  1. How about if you simply use if / else statement like this:

    <span class="value"><?php $assist=get_post_meta($post->ID, 'assistedLiving' , true); if ($assist=="") echo "keine Daten"; else if(strtolower($assist)=='true') echo "Ja"; else if(strtolower($assist)=='false') echo "Nein"; ?>&nbsp;</span>
    

    Note:

    For the code above to work,values for assistedLiving must use true/false and not 1 or 0.

    Update

    This is my first attempt at modifying shortcodes, so I’m not sure if it will work.

    I’m also not sure if this is what you need:) – I modified it in a way so that you can specify a parameter fieldname and when used it should show only that field.

    Change your shortcode function to this:

    function immopress_fields_shortcode( $atts ) {
    
        $args = wp_parse_args( $atts, array (
            'echo' => FALSE,
            'fieldname' => ""
        ) );
    
        return immopress_the_fields( $args );       
    }
    add_shortcode( 'immopress_fields', 'immopress_fields_shortcode' ); 
    

    In function immopress_the_fields() after

    foreach ( $fields as $key => $value) {
    

    add this:

    if ($fieldname!="")//only look for a field when it is used in the shortcode
      if ($fieldname!=$key)//skip until you find the value in the shortcode 
        continue;
    

    Not that this code is not efficient since $fields gets filled with all the fields all the time. Modified code is just a workaround.

  2. I recommend learning Smarty and then your code will be:

     <span class="value">
       {if get_post_meta($post->ID, 'assistedLiving' , true)}
         Yah
       {else}
         NEIN
       {/if}
     </span>
    

    To create this output:

    <span class="value">Yah</span>