1

I created several custom fields for my custom post type "books"

There is a checkbox for languages. Whenever a language gets checked, several custom fields for that language get added (i.e. title, cover image, isbn).

When displaying the content of a "book", I'd like to loop through my custom fields and display the one's that belong together in a collapsible panel. Here is what I have so far:

           <?php $fields = get_field_objects();
             if ($fields): ?>
             <ul>
              <?php
                foreach ($fields as $name => $field):
                  if (stripos($name, 'isbn') !== false) : ?>
                     <button data-toggle="collapse" data-target="#demo">Deutsch/<?php echo substr($field['label'], strpos($field['label'], " ") + 1); ?></button>
                     <div id="demo" class="collapse">
                     <?php //isbn
                        echo $field['prepend'] . " " . $field['value']; ?> </br>
                        <?php echo the_field('titel_'.substr($field['label'], strpos($field['label'], " ") + 1)); ?>

                      </div>

                   <?php endif;
                 endforeach; ?>
                 </ul>
              <?php endif; ?>

the buttons for the collapse are displaying the correct data (i.e. Deutsch/Englisch, Deutsch/Arabisch) but the title and ISBN both belong to the first language displayed.

I would appreciate any hints!

the var_dump() of the $fields variable: EDIT removed the var_dump since it was way too big!

Edit

Here is the update of my code: It's now displaying the correct fields and data, but I don't know how to put it in the collapsible panel..

 <b>Erhältlich in den folgenden Sprachausgaben:</b>

        <?php
        $fields = get_field_objects();
        if ($fields): ?>
            <br>
            <?php
            foreach ($fields as $name => $field): ?>

                <?php //hoerprobe
                if (get_field('horprobe_verfuegbar') == 1) :
                    if (stripos($name, 'hoerprobe') !== false) : ?>
                        <?php echo $field['label'];
                        $file = get_field($name); ?>
                        <audio controls>
                            <source src="<?php echo $file['url']; ?>">
                            Your browser does not support the audio tag.
                        </audio>
                        </br>

                    <?php endif;
                else :
                    // echo 'false';
                endif; ?>

                <?php
                if (stripos($name, 'isbn') !== false) : ?>
                    <button data-toggle="collapse" data-target="#demo">
                        Deutsch/<?php echo substr($field['label'], strpos($field['label'], " ") + 1); ?></button>

                    <?php //isbn
                    echo $field['prepend'] . " " . $field['value']; ?> </br>

                    <?php //titel
                    echo the_field('titel_' . substr($field['name'], strpos($field['name'], "_") + 1)); ?> </br>

                    <?php //coverimage
                    $titelbild = get_field('titelbild_' . substr($field['name'], strpos($field['name'], "_") + 1)); ?>
                    <?php if ($titelbild) : ?>
                        <img src="<?php echo $titelbild['url']; ?>" alt="<?php echo $titelbild['alt']; ?>"/>
                    <?php endif; ?></br>
                <?php endif;
            endforeach;
        endif; ?>

when adding this

<div id="demo" class="collapse">
                       ...</div>

no matter which button I click, it's only opening the data for the first language..

Edit 2

Audio file is nor working inside the collapse panel. Here is how it works:

 <b>Erhältlich in den folgenden Sprachausgaben:</b>

        <?php
        $fields = get_field_objects();
        if ($fields): ?>
            <br>
            <?php
            foreach ($fields as $name => $field): ?>

                <?php //hoerprobe

                if (get_field('horprobe_verfuegbar') == 1) :

                    if (stripos($name, 'hoerprobe') !== false) : ?>

                        <?php echo $field['label'];
                        $file = get_field($name); ?>
                        <audio controls>
                            <source src="<?php echo $file['url']; ?>">
                            Your browser does not support the audio tag.
                        </audio>
                        </br>

                    <?php endif;
                else :
                    // echo 'false';
                endif; ?>
                <?php
                if (stripos($name, 'isbn') !== false) : ?>
                    <?php $lang = substr($field['name'], strpos($field['name'], "_")); ?>
                    <button data-toggle="collapse" data-target="#demo<?php echo $lang ?>">
                        Deutsch/<?php echo substr($field['label'], strpos($field['label'], " ") + 1); ?> </button>

                    <div id="demo<?php echo $lang ?>" class="collapse">
                        Lorem ipsum dolor text....

                        <?php //isbn
                        echo $field['prepend'] . " " . $field['value']; ?> </br>

                        <?php //titel
                        echo the_field('titel_' . substr($field['name'], strpos($field['name'], "_") + 1)); ?> </br>

                        <?php //coverimage
                        $titelbild = get_field('titelbild_' . substr($field['name'], strpos($field['name'], "_") + 1)); ?>
                        <?php if ($titelbild) : ?>
                        <img src="<?php echo $titelbild['url']; ?>" alt="<?php echo $titelbild['alt']; ?>"/>
                        <?php endif; ?></br>
                    </div>
                <?php endif;
            endforeach;
        endif; ?>

but as soon as I move the hoerprobe part inside the collapse panel, it's not displaying it anymore..

6
  • could you post values inside $fields. will be better to use this echo "<pre>"; print_r($fields); echo "/<pre>";
    – user145078
    Jul 25, 2018 at 17:44
  • sorry I don'T know what you mean or where it'S leading me?
    – sonja
    Jul 26, 2018 at 7:33
  • Are you able to show us the values in the $fields array? For example by using var_dump($fields); This might help shed some light on the problem? Possibly what @LatheeshVMVilla was asking for?
    – junkrig
    Jul 26, 2018 at 7:46
  • sure! It's a pretty long dump, so I don't know if I should just post it all.. I'll add it to my post @junkrig
    – sonja
    Jul 26, 2018 at 7:52
  • In dump I see for example $fields["titel_arabisch"] item, but you are trying get <?php echo the_field('titel_Arabisch'); ?>. Maybe it should be echo the_field('titel_'.substr($field['name'], strpos($field['name'], "_") + 1))
    – nmr
    Jul 26, 2018 at 8:39

1 Answer 1

1

You display data in loop, so all item have the same ID div id="demo". Add some suffix to make them unique. For example:

$lang = substr($field['name'], strpos($field['name'], "_");

echo '<button data-toggle="collapse" data-target="#demo-'. $lang .'"> ....';

echo '<div id="demo-'. $lang .'" class="collapse"> ...</div>'

Edit

Audio doesn't work inside the collapse panel because key $name never meets both conditions:

if (stripos($name, 'isbn') !== false): 
   if (stripos($name, 'hoerprobe') !== false)

$name is either hoerprobe_arabisch or isbn_arabisch.

Try this code:

if (stripos($name, 'isbn') !== false) : ?>
   <?php $lang = substr($field['name'], strpos($field['name'], "_")); ?>
   <button data-toggle="collapse" data-target="#demo<?php echo $lang ?>">
      Deutsch/<?php echo substr($field['label'], strpos($field['label'], " ") + 1); ?> </button>

    <div id="demo<?php echo $lang ?>" class="collapse">
         Lorem ipsum dolor text....

       <?php //isbn
          echo $field['prepend'] . " " . $field['value']; ?> </br>

       <?php //titel
          echo the_field('titel'. $lang); ?> </br>

       <?php //coverimage
          $titelbild = get_field('titelbild' . $lang); ?>
          <?php if ($titelbild) : ?>
              <img src="<?php echo $titelbild['url']; ?>" alt="<?php echo $titelbild['alt']; ?>"/>
          <?php endif; ?></br>

       if (isset($fields['hoerprobe_'.$lang])) : ?>

           <?php
           $hoerprobe_field_name = 'hoerprobe'.$lang;
           $hoerprobe_field = $fields[$hoerprobe_field_name ];
           echo $hoerprobe_field['label'];
           $file = get_field(hoerprobe_field_name); ?>

           <audio controls>
               <source src="<?php echo $file['url']; ?>">
                    Your browser does not support the audio tag.
               </audio></br>
       <?php endif;

    </div>
<?php endif;
1
  • awesome thank you so much! Now everything is working the way I want except for the audio file. For some reason I can't put it in the collapse panel. It's only working outside the panel.. I added my code, do you see where the problem is?
    – sonja
    Jul 26, 2018 at 9:13

Your Answer

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

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