Home › Forums › Add-ons › Repeater Field › Query posts within taxonomy via repeater › Reply To: Query posts within taxonomy via repeater
Hmmmm…still did not work. HOWEVER, I decided to just re-do that whole section (the tab content area) from scratch and in doing so was able to get it to work using a different method of grabbing the posts. Instead of ‘while’, I did ‘foreach’. I’m still not sure why the other way wasn’t working. I feel like it *should* have. But here’s what I got working (I can’t take *all* the credit…my programming-genius friend did help me here, but he’s just not always available).
<!-- Nav Tabs -->
<ul class="nav nav-pills">
<?php if (have_rows('home_categories')) {
$i = 0;
while (have_rows('home_categories')) {
the_row();
$term = get_sub_field('categories'); ?>
<li class="<?php if ($i == 0) { echo ' active'; }?>">
<a href="#tab-pane-<?php echo $i; ?>" data-toggle="tab"><?php echo $term->name; ?></a>
</li>
<?php $i++;
}
} ?>
</ul>
<!-- Tab Content -->
<div class="tab-content clearfix">
<?php
$home_categories = get_field("home_categories");
// print_r($home_categories);
foreach($home_categories as $key => $home_cat) {
$term_id = $home_cat['categories']->term_id;
$term_name = $home_cat['categories']->name;
?>
<div class="tab-pane fade<?php if ($key == 0) { echo ' in active'; }?>" id="tab-pane-<?php echo $key; ?>">
<?php
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'terms' => array($term_id)
)
),
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'ASC',
);
$query = new WP_Query( $args );
foreach($query->posts as $post) {
?>
<a href="<?php echo get_permalink($post->ID); ?>"><?php echo get_the_post_thumbnail($post->ID, 'thumbnail', array('class' => 'img-responsive img-thumbnail')); ?></a>
<a href="<?php echo get_permalink($post->ID); ?>"><h3><?php echo get_the_title($post->ID); ?></h3></a>
<?php the_excerpt(); ?>
<?php
}
?>
</div>
<?php
} ?>
</div>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.