大概是简单的PHP如果/ else语句不起作用

时间:2014-05-21 05:36:34

标签: php if-statement

我似乎遇到了另一件似乎是超级基本PHP的问题,但它对我不起作用。

我的客户(房地产网站)需要能够拥有无价格的房产,无论是“要求价格”还是“拍卖”。目前,将价格字段留空仅允许一个。

我尝试更改以下代码:

$listing_price_labels = array(
‘sold’ => __( ‘Sold’, ‘wpsight’ ),
‘rented’ => __( ‘Rented’, ‘wpsight’ ),
‘request’ => __( ‘Price on request’, ‘wpsight’ ),
‘auction’ => __( ‘Auction’, ‘wpsight’ ), ***– Added this line***
);

找到此代码的位置......

if( is_admin() ) 
        $listing_price .= ‘<br />’ . wpsight_get_price_value();

    } elseif( empty( $listing_price ) ) {

        // When no price available Price on request
        $listing_price = ‘<span class=”listing-price-on-request”>’ . $listing_price_labels['request'] . ‘</span><!– .listing-price-on-request –>’;

    } elseif( $listing_price = ‘auction’ ) {

        // When price field contains ‘auction’ (case sensitive)
        $listing_price = ‘<span class=”listing-price-on-request”>’ . $listing_price_labels['auction'] . ‘</span><!– .listing-price-on-request –>’;

    }



    function wpsight_get_price( $post_id = '' ) {

        // Get post ID from $post_id

        if( empty( $post_id ) )
            $post_id = get_the_ID();    

        // If still empty, return false

        if( empty( $post_id ) )
            return false;

        // Set listing price labels

        $listing_price_labels = array(
            'sold'    => __( 'Sold', 'wpsight'  ),
            'rented'  => __( 'Rented', 'wpsight'  ),
            'request' => __( 'Price on request', 'wpsight' ),
            'auction' => __( 'Auction', 'wpsight' ),
            );

        $listing_price_labels = apply_filters( 'wpsight_get_price_labels', $listing_price_labels );

        // Get listing price
        $listing_price = wpsight_get_price_value();

        // Get custom fields

        $custom_fields          = get_post_custom( $post_id );
        $listing_status         = isset( $custom_fields['_price_status'][0] ) ? $custom_fields['_price_status'][0] : false;
        $listing_availability   = isset( $custom_fields['_price_sold_rented'][0] ) ? $custom_fields['_price_sold_rented'][0] : false;

        // Create price output

        if( ! empty( $listing_availability ) ) {

            // When listing is not available

            $sold_rented = ( $listing_status == 'sale' ) ? $listing_price_labels['sold'] : $listing_price_labels['rented'];

            // Display sold/rented bold red in admin
            $style = is_admin() ? ' style="color:red;font-weight:bold"' : false;

            $listing_price = '<span class="listing-price-sold-rented"' . $style . '>' . $sold_rented . '</span><!-- .listing-price-sold-rented -->';

            if( is_admin() ) 
                $listing_price .= '<br />' . wpsight_get_price_value();

        } elseif( empty( $listing_price ) ) {

            // When no price available Price on request
            $listing_price = '<span class="listing-price-on-request">' . $listing_price_labels['request'] . '</span><!-- .listing-price-on-request -->';

        } elseif( $listing_price == "auction" ) {

            // When price field contains 'auction' (case sensitive)
            $listing_price = '<span class="listing-price-on-request">' . $listing_price_labels['auction'] . '</span><!-- .listing-price-on-request -->';

        }

        return apply_filters( 'wpsight_listing_price', $listing_price );

    }

我确信我的语法一定是错的,因为有了这个代码,它会使任何写入价格字段的任何属性显示为“拍卖”。

谁能看到我做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试:

if( is_admin() ){
    $listing_price .= ‘<br />’ . wpsight_get_price_value();

    } elseif( empty( $listing_price ) ) {

    // When no price available Price on request
    $listing_price = ‘<span class=”listing-price-on-request”>’ . $listing_price_labels['request'] . ‘</span><!– .listing-price-on-request –>’;
}

顺便说一下,不推荐使用if( is_admin() ),因为它仅适用于1行。