Home › Forums › Front-end Issues › Extract the images of a custom post for each category › Reply To: Extract the images of a custom post for each category
For those interested in the feature, here’s how it worked for me.
function.php
// Loop Gallery Cat1
function my_custom_gallery_cat1() {
query_posts(array(
'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">
<?php
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>
</a>
</div>
<?php endwhile; ?>
</div> <?php
}
add_shortcode('example-terms', 'my_custom_gallery_cat1');
single-example.php
<div class="container-fluid">
<div class="row">
<?php echo do_shortcode('[categoria]');?>
</div>
</div>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.