Tell us what you are trying to do?
Minesh was kind enough to help me create this custom code: https://toolset.com/forums/topic/whatsapp-link-click-to-chat-shortcode/#post-1211733
function func_show_fixo( $atts ) {
global $post;
$numberfixo = get_post_meta($post->ID,'wpcf-fixo',true);
$numberfixo2 = $numberfixo;
$numberfixo = preg_replace('/\D+/', '', $numberfixo);
if(!empty($numberfixo)){
$linkfixo = '<img class="alignleft wp-image-350" src="/wp-content/uploads/2019/09/fixo.png" alt="phone-icon" width="25" height="25" /><a href="tel:'.$numberfixo.'">'.$numberfixo2.'</a>';
return $linkfixo;
}
}
add_shortcode('show_fixo','func_show_fixo');
This code works great for single custom number field. Now I just need to understand and implement for a repeating field. I know it has to do with arrays but I have no clue how to do it.
The only thing I need help with is to get the numbers from the repeating field (slug "fixo") and put them in another variable (total 2), let's say numberf1 and numberf2.
Something like this:
//get numbers from array below
$numberfixo = get_post_meta($post->ID,'wpcf-fixo',true);
$numberf1 = first number;
$numberf2 = second number;
The rest I can probably do.
Thank you
Hello,
I assume we are talking about a custom multiple instance numeric field "fixo", you can use WordPress function get_post_meta() to get all instance values as a PHP array, then run a loop on the PHP array to output the results, for example
$numberfixo_array = get_post_meta($post->ID,'wpcf-fixo', false);
foreach($numberfixo_array as $numberfixo){
...
}
More help:
https://developer.wordpress.org/reference/functions/get_post_meta/
$single
(bool) (Optional) If true, returns only the first value for the specified meta key. This parameter has no effect if $key is not specified.
Default value: false
enlace oculto
The foreach construct provides an easy way to iterate over arrays.
Thank you so much for the links, I have reviewed and tried to implement list() as shown in the video: enlace oculto
This is the code I put together:
//fixo
function func_show_fixo( $atts ) {
global $post;
$numberfixo_array = get_post_meta($post->ID,'wpcf-fixo', false);
foreach($numberfixo_array as list($a, $b)){
$a1 = $a;
$b1 = $b;
$a = preg_replace('/\D+/', '', $a);
$b = preg_replace('/\D+/', '', $b);
if(!empty($a) || !empty($b)){
if(!empty($b)){
$linkfixoAB = '<img class="alignleft wp-image-350" src="/wp-content/uploads/2019/09/fixo.png" alt="phone-icon" width="25" height="25" /><a href="tel:'.$a.'">'.$a1.' / </a><a href="tel:'.$b.'">'.$b1.'</a>';
return $linkfixoAB;
}
else {
$linkfixoA = '<img class="alignleft wp-image-350" src="/wp-content/uploads/2019/09/fixo.png" alt="phone-icon" width="25" height="25" /><a href="tel:'.$a.'">'.$a1.'</a>';
return $linkfixoA;
}
}
}
}
add_shortcode('show_fixo','func_show_fixo');
But unfortunately it is not showing. I must be doing something wrong.
Since it is a custom PHP codes problem, if you need more assistance for it, please provide a live test site with the same problem, fill below private message box with your website credentials and FTP access, also point out the problem page URL and where I can edit your PHP codes, I need a live website to test and debug it. Thanks
Thanks for the details, I have modified your PHP codes as below:
function func_show_fixo( $atts ) {
global $post;
$numberfixo_array = get_post_meta($post->ID,'wpcf-fixo', false);
$res = '<ul>';
foreach($numberfixo_array as $numberfixo){
$res .= '<li>';
$a = preg_replace('/\D+/', '', $numberfixo);
if(!empty($a)){
$linkfixo = '<img class="alignleft wp-image-350" src="/wp-content/uploads/2019/09/fixo.png" alt="phone-icon" width="25" height="25" /><a href="tel:'.$a.'">'.$a.' / </a><a href="tel:'.$numberfixo.'">'.$numberfixo.'</a>';
}
else {
$linkfixo = '<img class="alignleft wp-image-350" src="/wp-content/uploads/2019/09/fixo.png" alt="phone-icon" width="25" height="25" /><a href="tel:'.$numberfixo.'">'.$numberfixo.'</a>';
}
$res .= $linkfixo;
$res .= '</li>';
}
$res .= '</ul>';
return $res;
}
add_shortcode('show_fixo','func_show_fixo');
Please test again, check if it is fixed, thanks
Luo, your code was repeating the phone number twice, but it gave me an idea to try something which worked but there is a notice when there is only one number.
I believe it's when the second variable is empty in the array, it gives that notice. Everything else works fine with the code below:
//fixo
function func_show_fixo( $atts ) {
global $post;
$numberfixo_array = get_post_meta($post->ID,'wpcf-fixo', false);
if(!empty($numberfixo_array)){
list($a,$b) = $numberfixo_array;
$a1 = preg_replace('/\D+/', '', $a);
$b1 = preg_replace('/\D+/', '', $b);
if(!empty($b)){
$linkfixo = '<img class="alignleft wp-image-350" src="/wp-content/uploads/2019/09/fixo.png" alt="phone-icon" width="25" height="25" /><a href="tel:'.$a1.'">'.$a.' / </a><a href="tel:'.$b1.'">'.$b.'</a>';
}
else {
$linkfixo = '<img class="alignleft wp-image-350" src="/wp-content/uploads/2019/09/fixo.png" alt="phone-icon" width="25" height="25" /><a href="tel:'.$a1.'">'.$a.'</a>';
}
}
return $linkfixo;
}
add_shortcode('show_fixo','func_show_fixo');
Here is a video explaining: enlace oculto
I have changed the PHP codes as below:
function func_show_fixo( $atts ) {
global $post;
$numberfixo_array = get_post_meta($post->ID,'wpcf-fixo', false);
$arr = array();
foreach($numberfixo_array as $numberfixo){
$a = preg_replace('/\D+/', '', $numberfixo);
if(!empty($a)){
$linkfixo = '<a href="tel:'.$a.'">'.$numberfixo.'</a>';
}
$arr[] = $linkfixo;
}
$res = '<img class="alignleft wp-image-350" src="/wp-content/uploads/2019/09/fixo.png" alt="phone-icon" width="25" height="25" />' . implode('/', $arr);
return $res;
}
add_shortcode('show_fixo','func_show_fixo');
It will output the phone icon only once, and output the numbers with separator "/"。
It is just an example for your reference.
More help:
enlace oculto
Luo is the man! My issue is resolved now. Thank you!