简体   繁体   English

为wordpress中的高级自定义字段写一个php function

[英]Writing a php function for advance custom field in wordpress

i want to implement a progress bar using elementor for wordpress website, for that i've used the widget progress bar, but i'm adding the percentage value dynamically in the progress value, for that i've created a field percentage in my Campaign Details group field in Advance custom field plugin, which have type number and added it to the dynamic tag in progress bar.我想使用 elementor 为 wordpress 网站实现一个进度条,因为我使用了小部件进度条,但我在进度值中动态添加百分比值,为此我在我的活动中创建了一个字段百分比Advance 自定义字段插件中的详细组字段,具有类型编号并将其添加到进度条中的动态标签。 After that i've added my php code in snipped code, i'm new to php, so dont know why it is incorrect, i did take the help of chatgpt other sources to find the mistake.之后我在截取的代码中添加了我的 php 代码,我是 php 的新手,所以不知道为什么它不正确,我确实在 chatgpt 其他来源的帮助下找到了错误。

i've wrote two types of function, in first type, i've used the array to access the campaign details group field to access its fields and in another i've used this notation get_field('group_field_name_subfield_name')我写了两种类型的 function,在第一种类型中,我使用数组访问活动详细信息组字段以访问其字段,在另一种类型中我使用了这种表示法get_field('group_field_name_subfield_name')

first type第一类

function calculate_donation_percentage($post_id) {
    $donation_received = get_field('Campaign Details', $post_id)['donation_received'];
    $donation_required = get_field('Campaign Details', $post_id)['donation_required'];

    if ($donation_required == 0) {
        $percentage = 0;
    } else {
        $percentage = ($donation_received / $donation_required) * 100;
    }

    update_field('Campaign Details', array('donation_percentage'=> $percentage), $post_id);
}
add_action('save_post', 'calculate_donation_percentage');

second type第二种

function calculate_donation_percentage($post_id) {
    $donation_received = get_field('Campaign Details_donation_received', $post_id);
    $donation_required = get_field('Campaign Details_donation_required', $post_id);

    if ($donation_required == 0) {
        $percentage = 0;
    } else {
        $percentage = ($donation_received / $donation_required) * 100;
    }

    update_field('Campaign Details_donation_percentage', $percentage, $post_id);
}
add_action('save_post', 'calculate_donation_percentage');

i've read that, we can add filter method too, for adding the value into acf field我读过,我们也可以添加过滤方法,用于将值添加到 acf 字段中

add_filter('acf/update_value/key=campaign_details_donation_percentage', 'calculate_donation_percentage', 10, 3);

please give me some idea, how i solve this problem.请给我一些想法,我是如何解决这个问题的。 这是我创建的 acf 字段的图像

In first type, I think you typed the wrong field ID 'Campaign Details', IDs does not contain space, check your acf dashboard to find the right field ID, check the attached image.在第一个类型中,我认为您输入了错误的字段 ID“活动详细信息”,ID 不包含空格,请检查您的 acf 仪表板以找到正确的字段 ID,并检查所附图像。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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