Last active
March 3, 2023 17:05
-
-
Save Garconis/ccade575c079bfd9f5526635764e646e to your computer and use it in GitHub Desktop.
Divi | Shortcode to create dynamic number counter with a custom field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// https://www.youtube.com/watch?v=y1hHhUT_mfM | |
// example usage: [fs_dynamic_number_counter number_field="counter_1" title="Counter 1" percent="" id="" class=""] | |
// set the number_field value to be whatever the custom field name is that has your counter number | |
// set the title value to be whatever you want the counter's title to be | |
// set the percent value to "off" if you don't want the percent sign, otherwise leave it blank/empty for it to show | |
// set the id or class values if you want either of those on your module | |
// e.g., if you add a class, you could use it to add $ or + before or after the number value via CSS (by adding these to your stylesheet) | |
// e.g., .counter-dollar-before .percent-value::before { content:'$'; } | |
// e.g., .counter-plus-after .percent-value::after { content:'+'; } | |
add_shortcode('fs_dynamic_number_counter', function ($atts = []) { | |
$atts = array_change_key_case((array)$atts, CASE_LOWER); | |
// set the default settings for the shortcode attributes | |
$a = shortcode_atts([ | |
'number_field' => '', | |
'title' = '', | |
'id' => '', | |
'class' => '', | |
'percent'=> '' | |
], $atts ); | |
// get the counter number that we specified in the shortcode itself | |
$counter_number = get_field( $a['number_field']); | |
// $counter_number = get_field("counter_1"); | |
$et_counter = ''; | |
$et_counter .= '[et_pb_circle_counter title="' . $a["title"] . '" number="' . $counter_number . '" percent_sign="' . $a["percent"] . '" module_id="' . $a["id"] . '" module_class="' . $a["class"] . '"][/et_pb_circle_counter]'; | |
return do_shortcode($et_counter); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment