0

I have a custom post type for architectural projects which includes, among other things, an ACF repeater which contains an image, its title, and its description. This repeater is turned into a slideshow on the individual project (post) page.

Each project is also assigned to one or more categories (housing, medical, etc.) On the category page for these projects, I want to pull down the first image for each project to use as a thumbnail.

My code in the category template looks something like this:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<pre><?php print_r( get_field('images') ) ?> </pre><!-- for debugging -->

    <div class="col-xs-4 col-md-2">
        <figure class="show-caption">

        <?php if( have_rows('images') ): the_row(); ?>
            <?php $photo = get_sub_field('photo'); $filename = $photo['sizes']['large']; ?>
            <a href="<?php the_permalink() ?>" class="thumbnail"><img src="<?= $filename ?>" width="250" height="166" alt=""></a>
        <?php endwhile; endif; ?>

        </figure>
    </div>

<?php endif; ?>

The problem is, have_rows('images') returns false on the category template page, but true on the project page. And when I print_r( get_field('images') ) on both pages, I get the full array of images on the project page, but only an integer (equal to the array size) on the category page.

How can I see the full array of images on the category page?

0

Finally found this solution on the ACF support forums. All I had to do was add this to my functions.php file in my custom theme:

//Include CPT In Categories Pages
function namespace_add_custom_types( $query ) {
    if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
        $post_types = get_post_types( '', 'names' ); 
        $query->set( 'post_type', $post_types);
        return $query;
    }
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types');

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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