0

i want to show image gallery in archives.php or category.php. The featured image and text content is showing but not image gallery. Below is the code inside category.php. I have tested to show one post from category 'blog'.

$args = array(
        'post_type' => 'post',
        'post_status' => 'any',
        'cat'=>3,
             'meta_query'=>
       array('relation'=>'AND',
         array(
        'key'=>'intro_post','value'=>'intro','type'=>'CHAR','compare'=>'LIKE'
        )
       )
   );
   $arr_posts = new WP_Query( $args );?>
    <?php  if (  $arr_posts->have_posts() ) : ?>
             <?php  while ( $arr_posts->have_posts() ) : $arr_posts->the_post(); ?>
                  <div class="entry-content">
                        <?php if (has_post_thumbnail()): ?>
                        <figure>
                        <?php the_post_thumbnail('full');?>
                        </figure>
                       <?php endif; ?>
        <?php the_content(); ?>

        </div><!-- .entry-content -->
                 <?php  endwhile; ?>
          <?php else : ?>
               <?php get_template_part( 'template-parts/content', 'none' ); ?>
          <?php endif; 
           wp_reset_query();
0

If posts have post_format is gallery, you can get all gallaries by used: get_post_gallery_images(get_the_ID()); See more: https://pippinsplugins.com/retrieving-image-urls-of-galleries/

  • thanks for your reply. The post_format is set to image. I first tried get_post_gallery_images(1044),and print_r. It returns empty array but in single page or single post file, it returns urls array. I think it's the category.php that is preventing gallery (how to fix this). The code in the link u gave me, do i put in functions.php or the category.php. – Phyo May 9 '18 at 10:42
  • I fixed it. I set query 'post_type' to 'attachment' and 'post' in pre_get_posts hook. So now image galleries are showing in archive or category. – Phyo May 9 '18 at 18:05
  • I am using the answer code in my theme and it's working well. Thank for your reply – huykon225 May 10 '18 at 7:18
0

I fixed it. I set query 'post_type' to 'attachment' and 'post' in pre_get_posts hook. So now image galleries are showing in archive or category. 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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