0

My posts have a particular custom field, price. Every time the value of this custom field is displayed on the front-end of my site, I want to prepend a small amount of text ("Starting from: "). The additional stipulation is that this prepend occurs only if the post in-question has a separate custom field (status) value equal to the string rent. How would/could I best approach this?

This custom field value is added to several different pages on my site, by means of several different PHP templates. Ideally, I'd like do this via my child theme's functions.php. Is there a hook I can use that would ensure this particular custom field always has text prepended?

Possibility 1

Someone suggested this https://wordpress.stackexchange.com/questions/121679/how-to-hook-a-filter-to-catch-get-post-meta-when-alternate-a-custom-field-output. But my potential issue with that is that this particular custom field value is a number, that is eventually formatted to price when rendered onto the page. So I'm not sure if editing the field value before that formatting (as described in the question) will cause a later issue with the price formatting.

Possibility 2

Someone else suggested I do this in-part with CSS:

.cust-field-class:before{content:'Text that you want '}

This rule may work for the text prepend portion, but not for the logic. i.e. How would I ensure this CSS rule is applied only to the desired posts?

Example Code - Here is a one function from my parent theme that is responsible for outputting this custom field value to the page. This is one function of several.

function houzez_listing_price() {

    $sale_price = get_post_meta( get_the_ID(), 'fave_property_price', true);
    $second_price     = get_post_meta( get_the_ID(), 'fave_property_sec_price', true );
    $price_postfix = get_post_meta( get_the_ID(), 'fave_property_price_postfix', true);
    $price_prefix  = get_post_meta( get_the_ID(), 'fave_property_price_prefix', true );

    $output = '';
    $price_as_text = doubleval( $sale_price );
    if( !$price_as_text ) {
        $output .= '<span class="item-price item-price-text">'.$sale_price. '</span>';
        return $output;
    }

    if( !empty( $price_prefix ) ) {
        $price_prefix = '<span class="price-start">'.$price_prefix.'</span>';
    }

    if (!empty($sale_price)) {

        if (!empty($price_postfix)) {
            if( empty( $second_price ) ) {
                $price_postfix = '&#47;' . $price_postfix;
            } else {
                $price_postfix = '';
            }
        }

        return $price_prefix.' '.houzez_get_property_price($sale_price) . $price_postfix;

    }
}
  • "is eventually formatted to price when rendered onto the page." How does this happen? What's the code for the value being output onto the page already? – Jacob Peattie Apr 18 '18 at 12:13
  • @JacobPeattie The value is outputted to several different pages, by means of several different PHP templates. That's why I was hoping for a more 'global' solution that would prepend the text regardless of PHP template. – cag8f Apr 18 '18 at 12:23
  • It depends on how those templates do the outputting. We'd need to see to be able to say though. – Jacob Peattie Apr 18 '18 at 12:26
  • @JacobPeattie OK so you're saying I can't do this without first tracking down every PHP template that outputs this field? That was the hopeful goal of this exercise--to avoid having to locate each instance of this custom field value. – cag8f Apr 18 '18 at 12:30
  • No. I didn't say that. I'm saying that I assume that each template uses the same method to output the value and if you shared one example of that code from the templates then it would be possible to say whether or not what you want is possible. FWIW it probably isn't possible, but I wouldn't be comfortable saying that for certain without seeing the exact implementation. – Jacob Peattie Apr 18 '18 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.