0

I'm trying to fix an issue that requires multiple array queries inside a loop. I'm no PHP whiz, but I got one part to work. I can't get the other part to show correctly. I'm using a collapse repeater field to show additional rows of data on custom posts.

I tried using a counter and a $uniqueid for the 2 arrays, but it's not working. Whoever coded this original page also made it a shortcode, so not sure if that's also causing an issue.

<?php 
function owl_shortcode($atts) {
    ob_start();

    extract(shortcode_atts (array(
        'category' => '',
        'name' => '',
        'orderby' => 'title'    
    ), $atts));

    if ($category) {
        $cats = explode(",", $category);    
        $array = array();
        for ($i = 0; $i < count($cats); ++$i) {
        $array[] = get_category_by_slug($cats[$i])->term_id;
        }

        $custom_query = new WP_Query( array( 
            'category__and' => $array,
            'post_type' => 'owl',
            'orderby'   => $orderby,
            'order'     => 'ASC',
            'posts_per_page' => -1 
        )); 
    }
    else {
        $custom_query = new WP_Query( array( 
            'post_type' => 'owl',
            'orderby'   => $orderby,
            'order'     => 'ASC',
            'posts_per_page' => -1 
        ));         
    }
    ?>  

<div class="row">   
<ul class="none">

<?php if ( $custom_query->have_posts() ): while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>

        <div class="col-sm-6">
            <li class="card containFloat">
                <?php //image ?>
                <div class="circle">
                    <?php $image = get_field('image');
                    if( !empty($image) ): 
                        $size = 'thumbnail';
                        $thumb = $image['sizes'][ $size ]; ?>
                        <img src="<?php echo $thumb; ?>" alt="<?php echo $image['alt']; ?>"  /> 
                        <?php else: ?>
                        <img src="https://online.une.edu/wp-content/uploads/2018/06/blank-female-profile.jpg" alt="" height="150px" width="150px"/>
                        <?php endif; ?>
                </div>

<?php if (in_array("program-administrator", $cats) || in_array("program-manager", $cats) || in_array("program-staff", $cats)) { ?>
            <div class="containFloat">
                <h2 class="margin-top">
                    <?php the_title(); ?><?php if (get_field('degree')) echo ', ' . get_field('degree'); ?>
                    </h2>
                    <p><?php the_field('job_title'); ?></p>
                     <p><i class="fas fa-phone"></i> <?php the_field('phone'); ?> </p>
                     <p><a href="mailto:<?php the_field('email'); ?>"><i class="fas fa-envelope-open"></i> <?php the_field('email'); ?></a></p>
                    <br>
                    </div>  

<?php if (in_array("program-administrator", $cats) || in_array("program-manager", $cats)) { ?>                      
            <div class="clearBoth">

<!--- Accordion Expander - Shortcode Test - Program Administrator / Program Manager --->    
                <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                   <?php
                   //$uniqueid = get_sub_field( 'collapser' );
                   $uniqueid = uniqid('collapser');
                   $count = 0;
                   while ( have_rows( 'collapser' ) ) : the_row(); ?>
                   <div class="panel panel-default listbox">
                    <div class="panel-heading" role="tab" id="headingTwo">

                    <a data-toggle="collapse" data-parent="#accordion" href="#collapse-<?php echo esc_attr( $uniqueid ); ?>-<?php echo esc_attr( $count ); ?>" aria-expanded="false">
                       <?php the_sub_field( 'expand_row' ) ?>
                    </a>

                </div>
                <div id="collapse-<?php echo esc_attr( $uniqueid ); ?>-<?php echo esc_attr( $count ); ?>"
                    class="panel-collapse collapse" role="tabpanel">
                   <div class="panel-body">
                    <?php if (get_sub_field('description')) { ?>
                    <div class="panel-text"><b>More info</b><?php the_sub_field('description'); ?></div>
                    <?php } ?>
                    <?php if (get_sub_field('education')) { ?>
                    <div class="panel-text"><b>Education</b><?php the_sub_field('education'); ?></div>
                    <?php } ?>
                    <?php if (get_sub_field('expertise')) { ?>
                    <div class="panel-text"><b>Expertise</b><?php the_sub_field('expertise'); ?></div>
                    <?php } ?>
                    <?php if (get_sub_field('experience')) { ?>
                    <div class="panel-text"><b>Experience</b><?php the_sub_field('experience'); ?></div>
                    <?php } ?>
                    <?php if (get_sub_field('research')) { ?>
                    <div class="panel-text"><b>Research Interests</b><?php the_sub_field('research'); ?></div>
                    <?php } ?>
                    <?php if (get_sub_field('publications')) { ?>
                    <div class="panel-text"><b>Publications</b><?php the_sub_field('publications'); ?></div>
                    <?php } ?>
                    <?php if (get_sub_field('bio')) { ?>
                    <div class="panel-text"><b>Bio</b><?php the_sub_field('bio'); ?></div>
                    <?php } ?>
                </div>
            </div>
        </div>
<?php $count ++; ?>
<?php endwhile; ?>
</div>
                </div>
                <?php } ?> 

<?php } else { ?>
                    <div class="containFloat">
                        <h2><?php the_title(); ?></h2>
                        <p><?php the_field('job_title'); ?></p>
                        <p><i class="fas fa-phone"></i> <?php the_field('phone'); ?> </p>
                        <p><i class="fas fa-envelope-open"></i> <a href="mailto:<?php the_field('email'); ?>"> <?php the_field('email'); ?></a></p><br>
                    </div>


<!--- Accordion Expander - OWL.php Test---> 
                <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                   <?php
                   //$uniqueid = get_sub_field( 'collapser' );
                   $uniqueid = uniqid('collapser');
                   $count = 0;
                   while ( have_rows( 'collapser' ) ) : the_row(); ?>
                   <div class="panel panel-default listbox">
                    <div class="panel-heading" role="tab" id="headingTwoz">

                        <a data-toggle="collapse" data-parent="#accordion" href="#collapse-<?php echo esc_attr( $uniqueid ); ?>-<?php echo esc_attr( $count ); ?>" aria-expanded="false">
                        <?php the_sub_field( 'expand_row' ) ?>
                        </a>

                </div>
                <div id="collapse-<?php echo esc_attr( $uniqueid ); ?>-<?php echo esc_attr( $count ); ?>"
                    class="panel-collapse collapse" role="tabpanel">
                   <div class="panel-body">
                   <?php if (get_sub_field('description')) { ?>
                    <div class="panel-text"><b>More info</b><?php the_sub_field('description'); ?></div>
                    <?php } ?>
                    <?php if (get_sub_field('expertise')) { ?>
                    <div class="panel-text"><b>Expertise</b><?php the_sub_field('expertise'); ?></div>
                    <?php } ?>
                </div>
            </div>
        </div>
<?php $count ++; ?>
<?php endwhile; ?>

</div>
<!--- End Accordion --->    

                <?php } ?>

            </li>
        </div>

<?php endwhile; endif; ?>   
</ul>   
    </div>                   
<?php 
wp_reset_query(); 
return ob_get_clean();
}
?>

Page in question: http://dev-une-online-testing.pantheonsite.io/about/about-owl/academic-programs/

Page that works: http://dev-une-online-testing.pantheonsite.io/about/about-owl/

Any advice would be awesome.

  • $uniqueid is always going to be the same. You may need to pass the post id for your fields, especially for the collapser while loop. – MikeNGarrett Apr 4 at 21:03
  • Thanks for the advice. Where in the loop would the post ID go? code: <?php //$uniqueid = get_sub_field( 'collapser' ); $uniqueid = uniqid('collapser'); $count = 0; while ( have_rows( 'collapser' ) ) : the_row(); ?> – Chris Felax Apr 5 at 14:48
  • The second parameter for many ACF functions is the post id. This ensures you're explicitly referencing the correct object. – MikeNGarrett Apr 5 at 14:51
  • That makes sense, but am lost as to where to add this to the if array code! – Chris Felax Apr 5 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.