最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Multiple Arrays for Custom Post Query using Boostrap accordions and ACF repeater fields

matteradmin9PV0评论

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=".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: /

Page that works: /

Any advice would be awesome.

Post a comment

comment list (0)

  1. No comments so far