0

Is it possible to run a loop inside the do_shortcode() function?

Example:

echo do_shortcode('[iscorrect]'.$text_to_be_wrapped_in_shortcode.'[/iscorrect]');

http://codex.wordpress.org/Function_Reference/do_shortcode

I have tried creating a function to obtain the data and stick it in an array. Then for each item in that array, return the individual array value.

Example:

function the_ips(){
    $ips = get_ips();
    foreach($ips as $ip){
        return $ip; 
    }
}

I have dumped the array of data to be sure it has the correct data within it. Everything is correct. It continues to output the first value of the array within the do_shortcode() function, but nothing else.

Here is what I have tried:

echo do_shortcode('[iscorrect]'.the_ips().'[/iscorrect]');

or

$content = '';
$content .= '[iscorrect]';
$ips = get_ips();
foreach($ips as $ip){
    $content .= $ip;    
}
$content .= '[/iscorrect]';
echo do_shortcode($content);

It still continues to produce the first result of the array and nothing else.

1 Answer 1

0

Your call to return immediately returns from the function. The rest of the foreach loop never gets to run. Perhaps you just want to join the ips?

return implode(" ", $ips);

Or, as a list:

function the_ips(){
  $ips = get_ips();
  $output = "<ol>";
  foreach($ips as $ip){
    $output .= "<li>{$ip}</li>";
  }
  return $output .= "</ol>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ultimately, I would like to return each IP as a list item in an ordered list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.