php - 自定义字段 - 从真/假到是/否的 bool 值显示

标签 php wordpress

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

一个自定义字段的导入看起来像这样......

导入后的字段名称 ---> 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;
    }

但我只需要显示单个名称和值而不是所有导入字段。

简码的代码..

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 一直被所有字段填满。修改后的代码只是一种解决方法。

关于php - 自定义字段 - 从真/假到是/否的 bool 值显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16419759/

相关文章:

wordpress - WordPress崩溃,nginx和fastcgi问题?

php - 在它填充的多边形改变尺寸后计算新的渐变位置

php - WooCommerce:在结帐、我的帐户、管理订单和 WordPress 用户中添加出生日期计费字段

php - WordPress:按月分组的事件

wordpress - 如何在jmeter中播放视频文件

php - 运行 wood-starter-theme 时未找到类 'Timber'

javascript - 服务器响应问题

php - 在一列中显示一半数据,在另一列中显示另一半数据

php - 将对象转换为数组

php - 如何根据用户输入从多个表的单个列中获取所有行?