Display ACF values from an array with %customfield()%

#587173
  • Resolved Anonymous
    Rank Math free

    Hello,

    Love the product and it works great for so many things.

    I store and display custom data in ACF fields in a custom post type. I’m using %customfield()% to display some of this data in the custom post type meta title and description under titles & meta.

    It works great for single values, but for some values I use select or checkbox for multiple fields. Is there a way of displaying either the first value in the array, or all values from the array split with spaces using the customfield thing?

Viewing 1 replies (of 1 total)
  • Hello,

    Thank you for your query and we are so sorry about the trouble this must have caused.

    With that variable, you can only output the values of text fields or other similar ones that include string values in the database.

    To output the data from more advanced fields you would need to create a custom variable using the following filter: https://rankmath.com/kb/filters-hooks-api-developer/#add-extra-variables

    The callback function would be responsible for performing the necessary calls and checks using PHP and then returning the values you need.

    Here’s an example of using this function to get the price of a WooCommerce product dynamically and associate it with the variable %price%:

    
    add_action( 'rank_math/vars/register_extra_replacements', function(){
        rank_math_register_var_replacement(
            'price', [
                'name'        => esc_html__( 'Price', 'rank-math' ),
                'description' => esc_html__( 'Output the price of the product', 'rank-math' ),
                'variable'    => 'price',
                'example'     => price_callback(),
            ],
            'price_callback'
        );
    });
    
    function price_callback() {
        $product = wc_get_product(get_queried_object_id());
    
        if( $product ) {
            $price_vat = wc_get_price_including_tax( $product );
            $price = number_format($price_vat, 2);
        }
    
        return $price;
    }
    

    Don’t hesitate to get in touch if you have any other questions.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 1 replies (of 1 total)

The ticket ‘Display ACF values from an array with %customfield()%’ is closed to new replies.