Reputation: 524
My code is mostly working but there's a small problem.
I am expecting to see this output...
Cats
Dogs
But instead I am getting:
Cats
Dogs
As you can see every post is being listed in each category, instead of just the posts for each (sorry - unintentional pun) respective category.
Here's is my code:
<?php
$cat_args = array(
'taxonomy' => 'animal_category'
);
$categories = get_categories($cat_args);
foreach ( $categories as $category ) {
$category_hero = get_field('hero', $taxonomy . '_' . $category->term_id); ?>
<div class="gallery">
<div class="gallery-hero">
<h2><?php echo $category->name; ?></h2>
<img src="<?php echo $category_hero["sizes"]["Full"]; ?>" />
</div>
<?php
$cat_ID = $category->id;
$post_args = array(
'showposts' => -1,
'post_type' => 'gallery',
'offset' => 0,
'category' => $cat_ID
);
$posts = get_posts($post_args);
foreach($posts as $post) { ?>
<div class="gallery-box">
<?php $gallery_image = get_field( "photos"); ?>
<a href="<?php the_permalink() ?>">
<img src="<?php echo $gallery_image[0]["sizes"]["Medium"]; ?>" />
<span><?php the_title(); ?></span>
</a>
</div>
<?php } ?>
</div>
Everything looks right to me. What am I doing wrong?
Upvotes: 0
Views: 224
Reputation: 595
You have an error on your code. Use with the following code. I Hope this will work correctly.
<?php
$cat_args = array(
'taxonomy' => 'animal_category'
);
$categories = get_categories($cat_args);
foreach ( $categories as $category ) {
$category_hero = get_field('hero', $taxonomy . '_' . $category->term_id); ?>
<div class="gallery">
<div class="gallery-hero">
<h2><?php echo $category->name; ?></h2>
<img src="<?php echo $category_hero["sizes"]["Full"]; ?>" />
</div>
<?php
$cat_ID = $category->term_id;
$post_args = array(
'showposts' => -1,
'post_type' => 'gallery',
'offset' => 0,
//'category' => $cat_ID
'tax_query' => array(
array(
'taxonomy' => 'animal_category',
'field' => 'id',
'terms' => $cat_ID
)
)
);
$posts = get_posts($post_args);
foreach($posts as $post) { ?>
<div class="gallery-box">
<?php $gallery_image = get_field( "photos"); ?>
<a href="<?php the_permalink() ?>">
<img src="<?php echo $gallery_image[0]["sizes"]["Medium"]; ?>" />
<span><?php the_title(); ?></span>
</a>
</div>
<?php } ?>
</div>
<?php } ?>
Upvotes: 1