1

I am using a child theme on genesis. I have set up a custom post type and a custom taxonomy, and I'm using Advanced Custom Fields to put a gallery on each page of the CPT. On the taxonomy archive pages, I would like to show a single image from each gallery as a thumbnail, linking to the post. I have successfully set this up using a new template for the custom taxonomy, but if I set 'numberposts' => -1 to show all the posts, I get eight repeats of each post/thumbnail. If I set it to 'numberposts' => 1, I get 3 repeats of each post/thumbnail. How do I show just a single thumbnail from each post?

I don't know for sure that this is a wordpress problem, but I think it must be something to do with how I am setting up the custom loop?

<?php
//* Template Name: Press Release; Archive

remove_action ('genesis_loop', 'genesis_do_loop'); // Remove the standard loop 
add_action ('genesis_after_loop', 'collection_categories'); // add custom loop
function collection_categories(){

$terms = get_terms('collection_categories');
foreach($terms as $term) {
$posts = get_posts(array(
        'post_type' => 'collections',
        'tax_query' => array(
            array(
                'taxonomy' => 'collection_categories',
                'field' => 'slug',
                'terms' => $term->slug
            )
        ),
        'numberposts' => 1
    ));

foreach($posts as $post) {

//* One image from gallery on archive pages

        if ( have_posts() ) : 
while ( have_posts() ) : the_post();

    $images = get_field('gallery'); 
    $image_1 = $images[0];
    $link = get_the_permalink();

?>

<a href="<?php echo $link ?>"><img src="<?php echo $image_1['sizes']    ['thumbnail']; ?>" alt="<?php echo $image_1['alt']; ?>" /></a>
<?php

        endwhile;
        endif;
}
}
}
genesis();
2
  • 1
    Why are you using a while loop inside another foreach loop?
    – Johansson
    Mar 18, 2018 at 16:37
  • 1
    Because I am not very good at php? :/ What should I use?
    – apol
    Mar 18, 2018 at 16:38

1 Answer 1

1

You are using a nested loop in your code. You should only use the foreach loop, and pass the post ID to get_field(). Here's an example:

$terms = get_terms( 
    [ 
        'taxonomy' => 'collection_categories', 
        'orderby'  => 'name', 
        'order'    => 'DESC'
    ] 
);

// Rest of the code  here 

foreach ( $posts as $post ) {

    //* One image from gallery on archive pages
    $images  = get_field( 'gallery', $post->ID ); 
    $image_1 = $images[0];

    ?>

    <a href="<?php the_permalink( $post->ID ); ?>">
        <img src="<?php echo $image_1['sizes']['thumbnail']; ?>" alt="<?php echo $image_1['alt']; ?>" />
    </a><?php

}

// Don't forget to reset the postdata
wp_reset_postdata();
6
  • So, when I try this, it no longer sorts the custom taxonomy - all posts show on each hierarchical page of the archive. It also shows everything twice...
    – apol
    Mar 18, 2018 at 16:46
  • I'm not sure why it sorted the posts in the first place, since you can't sort by terms or taxonomies. I've updated the answer to sort the get_terms() by their title.
    – Johansson
    Mar 18, 2018 at 16:51
  • Should I not be able to sort an archive by the taxonomy, such as: Tax 1 - Tax 1A - Tax 1B Tax 2 - Tax 2A - Tax 2B etc?
    – apol
    Mar 18, 2018 at 16:52
  • so, now I'm getting: Warning: Invalid argument supplied for foreach() Do I need to define the post somehow?
    – apol
    Mar 18, 2018 at 19:31
  • Did you remove the get_posts();? I removed it to shorten the answer, you should leave it where it was.
    – Johansson
    Mar 18, 2018 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.