简体   繁体   English

如何使用帖子类型在自定义页面上显示所有图像字段

[英]How to display all images fields on custom page with post type

I'm trying to loop through the fields of ACF to display all the images of a post type and a specific category我正在尝试遍历 ACF 的字段以显示帖子类型和特定类别的所有图像

I give a practical example: I have an ACF image field (image_of_project) and I want to view all the images uploaded in a post type (projects) associated with XYZ category我举一个实际例子:我有一个 ACF 图像字段 (image_of_project),我想查看与 XYZ 类别关联的帖子类型(项目)中上传的所有图像

Now I created this loop:现在我创建了这个循环:

' '

        $loop = new WP_Query( array( 
            'post_type' => 'projects',
            'posts_per_page' => 9,
            ) );
        
        while ( $loop->have_posts() ) : $loop->the_post();
        $images = get_field('image_of_project');
        if( $images ): ?>
                <?php foreach( $images as $image ): ?>
                
                        <div class="item">
                            <a href="<?php echo get_permalink( $post->ID ); ?>" target="_blank">
                            <?php echo wp_get_attachment_image( $image ); ?>
                            </a>
                        </div>
                <?php endforeach; ?>     
        <?php endif; ?>
        <?php endwhile; ?>

' '

The images are displayed but I do not understand why it duplicates me every single image and does not show me only the images of linked to a specific category.图像已显示,但我不明白为什么它会复制每张图像并且不只显示链接到特定类别的图像。

Thanks to anyone who will help me on this mission!感谢任何愿意帮助我完成这项任务的人!

For those interested in the feature, here's how it worked for me.对于那些对该功能感兴趣的人,以下是它对我的工作方式。

function.php函数.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单例.php

<div class="container-fluid">
 <div class="row">
  <?php echo do_shortcode('[categoria]');?>
 </div>    
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM