Home Forums Front-end Issues Extract the images of a custom post for each category


Extract the images of a custom post for each category

  • Hello everyone,

    I created this function here where it allows me to extract all the images of a CPT and to be displayed on another page.

    $loop = new WP_Query( array( 'post_type' => 'project') );
    while ( $loop->have_posts() ) : $loop->the_post();
    $images = get_field('gallery');
    if( $images ): ?>
    <?php foreach( $images as $image ): ?>
    <div class="item"><img src="<?php echo $image['sizes']['large']; ?>"  /></div>
    <?php endforeach; ?>
    <?php endif; ?>
    <?php endwhile; ?>

    In theory it works but what I miss is the possibility of being able to extract the images and be displayed for every single category available.

  • For those interested in the feature, here’s how it worked for me.


    // Loop Gallery Cat1
    function my_custom_gallery_cat1() {
            'post_type' => 'gd_project',
            'orderby' => 'rand',
            'posts_per_page' => 10,
            'tax_query' => array(
                array (
                    'taxonomy' => 'gd_projectcategory',
                    'field' => 'slug',
                    'terms' => 'example-terms',
        ?> <div class="masonry">
        while (have_posts()) : the_post();
            $image_field = get_field('image');
            $image_url = $image_field['url'];
            $image_alt = $image_field['alt']; ?>
            <div class="grid-item">
            <a href="<?php echo get_permalink( $post->ID ); ?>" target="_blank">
            <img src="<?php echo esc_url($image_url); ?>" alt="<?php echo esc_attr($image_alt); ?>" />
            <h4><?php the_field( 'title' ); ?></h4>
        <?php endwhile; ?>
        </div> <?php
    add_shortcode('example-terms', 'my_custom_gallery_cat1');


    <div class="container-fluid">
    <div class="row">
    <?php echo do_shortcode('[categoria]');?>
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.