简体   繁体   English

大概简单的PHP if / else语句不起作用

[英]Presumably simple PHP if/else statement not working

I seem to be having trouble with another piece of what seems to be super basic PHP, but it just won't work for me. 我似乎在处理似乎是超级基本PHP的另一部分时遇到了麻烦,但是它对我来说不起作用。

My client (real estate website) needs to be able to have properties with no price to be either “price upon request” OR “auction”. 我的客户(房地产网站)必须能够拥有无价格的房地产,才能成为“根据要求的价格”或“拍卖”。 Currently, leaving the price field blank only allows for one. 当前,将价格字段留为空白仅允许一个。

I tried changing the following code: 我尝试更改以下代码:

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

And where this code is found… 在哪里找到此代码...

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

    }

I'm sure my syntax must just be wrong, because with that code in place it makes any property with anything at all written into the price field display “auction”. 我确定我的语法一定是错误的,因为有了该代码,它会使任何写入价格字段的属性都显示为“拍卖”。

Can anyone see what I've done wrong? 谁能看到我做错了什么?

try: 尝试:

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

btw its not recommended to use if( is_admin() ) since its only applicable on 1 line. btw不建议使用if( is_admin() )因为它仅适用于1行。

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

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