0
我似乎遇到了另一块似乎是超级基本的PHP的问题,但它不适用于我。大概简单的PHP if/else语句不起作用
我的客户(房地产网站)需要能够有没有价格的属性是“价格应要求”或“拍卖”。目前,将价格字段留空仅允许一个。
我试图改变如下代码:
$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);
}
我敢肯定,我的语法必须仅仅是错误的,因为在地方,它的代码使任何写入价格栏显示“拍卖”的任何财产。
任何人都可以看到我做错了什么吗?
尝试'$ listing_price =='auction''而不是'$ listing_price ='auction''。实际上,您将字符串“auction”分配给变量“$ listing_price”,而不是将它们进行比较。在这种情况下,您也可以使用'===',除非值和类型匹配,否则不会返回true。更多信息:http://www.php.net/manual/en/language.operators.comparison.php – Andre
你可以在你的if语句之前回显'$ listing_price'的值吗? – Viscocent
$ listing_price。='
'。 wpsight_get_price_value();使它成为一个串联的字符串。 } elseif(empty($ listing_price)){即使由于断点返回false,也不会为空。你可以使用一个字段来说明它是否被出售等,并使用if条件。您也可以为每个条目应用条件显示逻辑。你是否循环着每一件物品? – user3587554