Skip Navigation

[Resuelto] Array for repeating field

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:

Use custom PHP codes to display multiple instance field values.

Solution:

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:

https://toolset.com/forums/topic/array-for-repeating-field/#post-1363091

Relevant Documentation:

https://developer.wordpress.org/reference/functions/get_post_meta/

This support ticket is created hace 5 años. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Este tema contiene 7 respuestas, tiene 2 mensajes.

Última actualización por FelipeP5703 hace 5 años.

Asistido por: Luo Yang.

Autor
Mensajes
#1362891

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

#1363091

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.

#1363621

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.

#1363957

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

#1365163

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

#1365629

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

#1366005

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

#1366273

Luo is the man! My issue is resolved now. Thank you!