Reputation: 181
In a taxonomy category I have:
Repeater field named questions. Inside a repeater I have a field named answer.
I am trying to show all answers in category.php file like this:
$term = get_queried_object();
if ( have_rows( 'questions', $term ) ) {
while( have_rows( 'questions', $term ) ) {
the_row();
the_sub_field( 'answer' );
}
}
It doesn't work. Can you please tell me what is wrong here? I have tried for hours to make it work.
Upvotes: 1
Views: 3978
Reputation: 2432
<?php
$queried_object = get_queried_object();
if( have_rows('product_category_faq_section_data',$queried_object) ){ ?>
<div class="panel-group" id="accordion">
<?php
$faq_count = 1 ;
?>
<?php while( have_rows('product_category_faq_section_data',$queried_object) ){ the_row(); ?>
<?php
$product_category_faq_title = get_sub_field('product_category_faq_title');
$product_category_faq_description = get_sub_field('product_category_faq_description');
if($faq_count == "1")
{
?>
<div class="panel panel-default">
<?php if($faq_count){?>
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse<?php echo $faq_count;?>">
<span class="glyphicon glyphicon-menu-right text-success"></span> <?php echo $product_category_faq_title;?>
</a>
</h4>
</div>
<?php }?>
<?php if($product_category_faq_description){?>
<div id="collapse<?php echo $faq_count;?>" class="panel-collapse collapse in">
<div class="panel-body"><?php echo $product_category_faq_description;?></div>
</div>
<?php }?>
</div>
<?php }else{?>
<div class="panel panel-default">
<?php if($faq_count){?>
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse<?php echo $faq_count;?>">
<span class="glyphicon glyphicon-menu-right text-info"></span> <?php echo $product_category_faq_title;?>
</a>
</h4>
</div>
<?php }?>
<?php if($product_category_faq_description){?>
<div id="collapse<?php echo $faq_count;?>" class="panel-collapse collapse">
<div class="panel-body"><?php echo $product_category_faq_description;?></div>
</div>
<?php }?>
</div>
<?php } ?>
<?php $faq_count = $faq_count +1 ; ?>
<?php } ?>
</div> <!-- end container -->
<?php } ?>
Upvotes: 0
Reputation: 81
If anyone's still looking for a solution, heres how I did it.
<?php
$term = get_queried_object();
$taxonomy = $term->taxonomy;
$term_id = $term->term_id;
if( have_rows('highlight_boxes', $taxonomy . '_' . $term_id) ):
while(have_rows('highlight_boxes', $taxonomy . '_' . $term_id)):
the_row();
?>
<div class="column">
<h2 class="heading-title"><?php the_sub_field('highlight_title', $taxonomy . '_' . $term_id) ?></h2>
<div class="description">
<p><?php the_sub_field('highlight_desc', $taxonomy . '_' . $term_id) ?></p>
</div>
</div>
<?php endwhile; endif; ?>
Upvotes: 8
Reputation: 181
I finally figured it out. I checked in phpmyadmin where the data is stored. It turned out that the data is saved in wp_termmeta and not as I thought in wp_postmeta. This is why most of the solutions didn't work.
Working workaround code for repeater added to taxonomy (a category in my example) using get_term_meta instead of ACF code (loops and functions).
<?php
// name of repeater field
$repeater = 'questions';
// get taxonomy id
$taxonomy_id = get_queried_object_id();
// get repeater data from term meta
$post_meta = get_term_meta($taxonomy_id, $repeater, true);
// count items in repeater
$count = intval(get_term_meta($taxonomy_id, $repeater, true));
// loop + apply filter the_content to preserve html formatting
for ($i=0; $i<$count; $i++) {
echo apply_filters('the_content', get_term_meta($taxonomy_id, $repeater.'_'.$i.'_'.'title', true));
echo apply_filters('the_content', get_term_meta($taxonomy_id, $repeater.'_'.$i.'_'.'answer', true));
}
?>
The solution from documentation still doesn't work for repeaters in taxonomy. It does work for non-repeaters (ex. image, text added to taxonomy). https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
Upvotes: 5
Reputation: 776
Have you tried looping through your field as an array instead? Often that's easier, especially if you are getting fields that aren't part of the global $post. Instead your setup would look like:
$questions = get_field( 'questions', $term );
foreach( $questions as $question ){
echo $question[ 'answer' ];
}
I find that much easier. I would guess your issue above has to do with the_row() not getting the global loop properly because you're accessing the fields of $term but I'm not sure. In any case - the code above should work.
Upvotes: 1