Category.php – add custom post contents to an array for each post
-
I am using a custom field on my posts to store an image. On the category.php page I would like to loop through all posts assigned to that category, pull out the custom image I have attached and add it to an array. With that array I will then place all the images into a gallery.
The code I have come up with seems to work for the most part, EXCEPT that it’s not adding the images to the same array, it’s creating a new array each time. At least that’s what I think is happening. Here’s what I’m using:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); $gallery_images = get_custom_field('catImage:to_array'); $thumbs_html = array(); foreach ($gallery_images as $galleryID) { $attachment = get_post( $galleryID ); $thumb_img = wp_get_attachment_image_src( $galleryID, 'thumbnail' ); //thumbnail src $full_img = wp_get_attachment_image_src( $galleryID, 'full' ); //full img src $thumbs_html[] = '<div class="indvlThumbCont"><a href="' . $full_img[0] . '" id="description-button-' . $gallery_images_count . '" class="thumbLink" target="_blank"><img class="thumbImg" src="' . $thumb_img[0] .'"></a></div>'; $gallery_images_count++; }//end forEach ?> <?php endwhile; else: ?> <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> <?php endif; ?> <?php echo implode("\n", $thumbs_html); ?>
Can anyone help?
- The topic ‘Category.php – add custom post contents to an array for each post’ is closed to new replies.