Last active
September 1, 2021 09:28
-
-
Save apsolut/b28b77f18cfc52f94ade10be80205a77 to your computer and use it in GitHub Desktop.
Loop ACF field taxonomy with multi selected taxes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* custom CPT `faqs` with taxonomy `faq_categories` | |
* section_faq_category is field Taxonomy with Term Object, multi select | |
* faq counter and definitions for terms | |
* loop taxonomy selected taxes with ACF | |
* tax_ID, array of IDs | |
* | |
*/ | |
$faq_object = get_sub_field('section_faq_category'); | |
$faq_count = 0; | |
if( $faq_object ): | |
$tax_id_all = array(); | |
foreach ( $faq_object as $faq_object_single ) { | |
$tax_id_convert = $faq_object_single->term_id; | |
$tax_id_all[] = $tax_id_convert; | |
} | |
$acf_all_tax_list = join(',', $tax_id_all); | |
$faq_query = new WP_Query( array( | |
'post_type' => 'faqs', | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'faq_categories', | |
'field' => 'term_id', | |
'terms' => array( $acf_all_tax_list ) | |
) | |
), | |
'post_status' => 'publish', | |
'posts_per_page' => $section_posts | |
) ); | |
$faq_count_posts = $faq_query->post_count; | |
?> | |
<div class="column relative posts-<?php echo $faq_count_posts; ?>"> | |
<?php while ( $faq_query->have_posts() ) : $faq_query->the_post(); | |
/** | |
* Make faq into columns | |
* calculate half of items , X = $faq_count_posts / 2 | |
* add column-break every X item | |
* if its ODD then add extra 1, so its divisible by 2 | |
*/ | |
$even = ($faq_count_posts % 2 == 0); | |
$odd = ($faq_count_posts % 2 != 0); | |
if($even){ | |
$faq_count_half = $faq_count_posts / 2; | |
} | |
if($odd){ | |
$faq_add_to_odd = $faq_count_posts + 1; | |
$faq_count_half = $faq_add_to_odd / 2; | |
} | |
if ($faq_count % $faq_count_half == 0) : | |
echo '<div class="column-break"></div>'; | |
endif; | |
$faq_count++; | |
?> | |
<div class="question accordion-item faq-item items-<?php echo $faq_count; ?> relative" data-accordion-item > | |
<?php the_field( 'faq_answer' ); ?> | |
</div> | |
<?php endwhile; ?> | |
</div> | |
<?php endif; wp_reset_query(); wp_reset_postdata(); ?> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.column { | |
column-count: 1; | |
break-inside: avoid; | |
@media only screen and (min-width: 1024px) { | |
column-count: 2; | |
} | |
.column-break { | |
break-after: auto; | |
break-inside: avoid; | |
@media only screen and (min-width: 1024px) { | |
break-after: column; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SqrhTRUH6S.mp4