WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following foreach witch gets all the content in several groups. I am using Magic Fields.

 <?php $omtaleseksjoner = get_group('Seksjoner');
foreach($omtaleseksjoner as $seksjoner){ 
    echo "<h3>" . $seksjoner['sectiontitle'][1] . "</h3>";
    echo $seksjoner['sectioncontent'][1];

I have usually 5 groups in this foreach, and I want to break it off in two places.

Is it possible to insert other content e.g. after group 2 and 4 ?


share|improve this question
up vote 3 down vote accepted

You can keep tracks of number of cycles and run extras when counter matches. Something like this:

$array  = array(1,2,3,4,5);

$i = 0;

foreach($array as $set) {

    if(2 == $i)
        echo 'after 2';

    if(4 == $i)
        echo 'after 4';

    echo $set;


I must note that this is more of PHP basics and really has nothing to do with WordPress. I highly recommend PHP documentation as first stop for such.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.