简体   繁体   中英

ACF Advanced Custom Field Multiplication returning 0 instead of the correct math answer

I am trying to do all manner of things with the ACF Advanced Custom fields. In this case I created two custom fields

<?php
$num1 = get_field('test_one');
$num2 =  get_field('test_two');
$num3 = $num1*$num2 ;
echo  "</br> Value is $num3" ; 
?>

test_one contains 2 as the default value, test_two contains 4 I am getting 0 as the result when clearly the field values should return something different.

What is going on here?

For multiplying Two numbers of Custom field value do as below:

1) add field in plugin page where you have to select type as "number" like this

2) Add below code in functions.php

function multiplynumber()
{
   $test_one = get_field('test_one', get_the_ID());
   $test_two = get_field('test_two', get_the_ID());
   $num3 = $test_two*$test_one ;
   echo  "</br> Value is".$num3 ; 
}

add_action('wp_head','multiplynumber');

You have to write post/page id where your field plugin showing in admin panel see this image where i have put condition in "page"

eg from backend in sample-page i have write value in "test_one" = 2 and "test_two" = 4 see here then it will show multiply value in that front page see here so you have to write that page ID in get_field function or you can also use

get_the_ID()

as dynamic purpose

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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