You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求协助编写WordPress中Classipress主题的价格格式化IF语句

Hey there! Let's fix that pricing formatting issue you're stuck with on your Classipress site. I get that you need different rules for BTC and USD prices, and your current code isn't handling that distinction—let's sort this out step by step.

First, let's recap your exact needs to make sure we're on the same page:

  • BTC prices: Always show 5 decimal places, no exceptions
  • USD prices: Hide decimal places only if they're all zeros (e.g., show 10 instead of 10.00000, but keep 10.12345 as-is)

Your original code didn't account for the currency type, which is why it was applying the same 5-decimal rule to everything. Here's a revised version that adds currency detection and applies your desired logic:

function cp_price_format( $price ) {
    global $cp_options;
    
    // First, handle global settings or zero price (keep your original logic)
    if ( $cp_options->hide_decimals || $price == 0 ) {
        return number_format( (float)$price, 0, $cp_options->decimal_separator, $cp_options->thousands_separator );
    }

    // Get the currency for the current ad (adjust the meta key if your theme uses a different one)
    // Classipress typically stores this in a custom field like 'cp_ad_currency'
    $currency = get_post_meta( get_the_ID(), 'cp_ad_currency', true );
    // Fallback to USD if no currency is found (adjust as needed)
    $currency = empty( $currency ) ? 'USD' : strtoupper( $currency );

    $decimal_sep = $cp_options->decimal_separator;
    $thousands_sep = $cp_options->thousands_separator;

    if ( is_numeric( $price ) ) {
        if ( $currency === 'BTC' ) {
            // BTC: Force 5 decimal places
            $price = number_format( $price, 5, $decimal_sep, $thousands_sep );
        } else {
            // USD: Check if decimals are all zero
            $price_float = (float)$price;
            if ( $price_float === floor( $price_float ) ) {
                // No decimals to show, return integer format
                $price = number_format( $price_float, 0, $decimal_sep, $thousands_sep );
            } else {
                // Keep decimals (here we use 5, but you can change to 2 if that's standard for USD)
                $price = number_format( $price, 5, $decimal_sep, $thousands_sep );
                // Optional: Trim trailing zeros (e.g., turns 10.50000 into 10.5)
                $price = rtrim( rtrim( $price, '0' ), $decimal_sep );
            }
        }
    }

    return $price;
}

Key changes explained:

  1. Currency detection: We pull the ad's currency from its custom field (I used cp_ad_currency which is common in Classipress—if your theme uses a different meta key, just swap that out).
  2. BTC handling: Straightforward—we force 5 decimal places every time, which matches your requirement.
  3. USD handling: We convert the price to a float and check if it's equal to its integer floor value (that tells us if all decimals are zero). If yes, we format it as an integer; if not, we keep the decimals (I kept 5, but you can change that to 2 if that's more standard for USD pricing).
  4. Optional trailing zero trim: The extra rtrim lines clean up unnecessary trailing zeros for USD (like turning 10.50000 into 10.5). If you want to keep all 5 decimals even when some are zero, just remove those two lines.

Why your previous fix didn't work:

Your modified code still used the original $decimals logic (based on global settings, not currency) and just converted the formatted string back to a float. That would strip decimals for all currencies when they're zero, which isn't what you wanted for BTC.

A quick note: Make sure the currency meta key matches what your Classipress theme uses. You can check this by editing an ad and inspecting the currency dropdown field's name attribute (it'll look like meta[cp_ad_currency] or similar).

内容的提问来源于stack exchange,提问作者Moonito

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:17:15