繁体   English   中英

自定义字段-布尔值显示是/否到是/否

[英]Custom Fields - Boolean Display of True/False to Yes/No

我正在从房地产网站导入字段。

导入一个自定义字段如下所示:

导入后的字段名称---> AssistedLiving字段值---> false

输出在WordPress的显示,我用这个...

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

网站上的结果看起来如此...

**Seniorengerechtes Wohnen:false**

如何显示False / True值以显示Yes / No或德语Ja / NEIN?

显示所有字段的功能(以“是/否”正确显示):

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;
    }

但是我只需要显示“单个名称和值”,而不是所有的Importet字段。

简码代码..

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' ); 

希望有人可以帮助我。

谢谢

托尼

如果您仅使用if / else语句,该怎么办:

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

注意:

为了使以上代码正常工作,AssistedLiving的值必须使用true / false,而不是1或0。

更新

这是我第一次尝试修改短代码,所以我不确定它是否会起作用。

我也不确定这是否是您所需要的:)-我以某种方式对其进行了修改,以便您可以指定参数fieldname,并且在使用时它应仅显示该字段。

将您的简码功能更改为此:

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' ); 

在函数immopress_the_fields()之后

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

添加:

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;

并不是因为$ fields一直被所有字段填充,所以此代码效率不高。 修改后的代码只是一种解决方法。

我建议学习Smarty ,然后您的代码将是:

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

要创建此输出:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM