我用ACF创建了一个可过滤的库。我的问题是,当我添加一个添加了多个分类法的项目时,在我的图库中,我拥有该项目的次数与添加分类法的次数相同。除此之外,我希望该项目只在库中出现一次,但出现在选择分类的所有过滤器中。
<div id="filters" class="col-3">
<ul class="nav">
<li data-filter="all" class="filter active">Tous les projets</li>
<?php
// 1. On définit les argument pour définir ce que l\'on souhaite récupérer
$args = array (
\'post_type\' => \'projet\'
);
$terms_id = [];
// 2. On execute la WP query
$my_query = new WP_Query( $args );
// 3. On lance la boucle
if ($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post();
$categories_du_projet = get_field( \'categories_du_projet\' );
if ( $categories_du_projet ) : ?>
<?php foreach ( $categories_du_projet as $term ) :
if (!in_array($term->term_id, $terms_id)) :
array_push($terms_id, $term->term_id);
?>
<li data-filter="<?= esc_html( $term->slug ); ?>" class="filter"><?= esc_html( $term->name ); ?></li>
<?php endif; endforeach; endif; endwhile; endif;
// 4. On réinitialise la requete principale
wp_reset_postdata();
?>
</ul>
</div>
<div id="elements" class="col-9">
<?php
// 1. On définit les argument pour définir ce que l\'on souhaite récupérer
$args = array (
\'post_type\' => \'projet\'
);
// 2. On execute la WP query
$my_query = new WP_Query( $args );
// 3. On lance la boucle
if ($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post();
$categories_du_projet = get_field( \'categories_du_projet\' );
$image_du_projet = get_field( \'image_du_projet\' );
$titre_du_projet = get_field( \'titre_du_projet\' );
$titre_du_projet = preg_replace(\'/\\s+/\', \'_\', strtolower($titre_du_projet));
$trigger_ID = \'trigger_\' . $arr_posts->current_post . \'_\' . get_the_ID();
$modal_ID = \'modal_\' . $arr_posts->current_post . \'_\' . get_the_ID();
$close_ID = \'close_\' . $arr_posts->current_post . \'_\' . get_the_ID();
$image_ID = \'grandeImage_\' . $arr_posts->current_post . \'_\' . get_the_ID();
$images = acf_photo_gallery(\'galerie_du_projet\', $post->ID);
if ( $categories_du_projet ) :
foreach ( $categories_du_projet as $term ) :
if ( $image_du_projet ) :
?>
<div class="<?= esc_html( $term->slug ); ?>">
<figure class="trigger" id="<?php echo $trigger_ID ?>">
<img src="<?php echo esc_url( $image_du_projet[\'url\'] ); ?>" alt="<?php echo esc_attr( $image_du_projet[\'alt\'] ); ?>" />
<figcaption><?php the_field( \'titre_du_projet\' ); ?></figcaption>
</figure>
</div>
<?php endif; ?>
<div class="modal" id="<?= $modal_ID ?>">
<div class="modal-content">
<span class="close-button" id="<?= $close_ID ?>">×</span>
<h2><?= get_field( \'titre_du_projet\' ); ?></h2>
<hr>
<div class="row">
<div class="image">
<?php $image_du_projet = get_field( \'image_du_projet\' ); ?>
<?php if ( $image_du_projet ) : ?>
<img class="<?= $image_ID; ?>" src="<?= esc_url( $image_du_projet[\'url\'] ); ?>" alt="<?= esc_attr( $image_du_projet[\'alt\'] ); ?>" />
<?php endif; ?>
<img class="grandeImage" />
<div class="row" id="gallery-full">
<?php
//Check if return array has anything in it
if( count($images) ):
//Cool, we got some data so now let\'s loop over it
foreach($images as $image):
$id = $image[\'id\']; // The attachment id of the media
$title = $image[\'title\']; //The title
$caption= $image[\'caption\']; //The caption
$full_image_url = $image[\'full_image_url\']; //Full size image url
//Resized size to 262px width by 160px height image url
$thumbnail_image_url= $image[\'thumbnail_image_url\']; //Get the thumbnail size image url 150px by 150px
$url= $image[\'url\']; //Goto any link when clicked
$target= $image[\'target\']; //Open normal or new tab
$alt = get_field(\'photo_gallery_alt\', $id); //Get the alt which is a extra field (See below how to add extra fields)
$class = get_field(\'photo_gallery_class\', $id); //Get the class which is a extra field (See below how to add extra fields)
?>
<div class="thumbnail">
<img id="image_<?= $id; ?>" src="<?= $full_image_url; ?>" alt="<?= $title; ?>">
</div>
<?php endforeach; endif; ?>
</div>
</div>
<div class="description">
<h3>Description</h3>
<p><?php the_field(\'description_du_projet\'); ?></p>
</div>
</div>
</div>
</div>
我想要的只是没有重复的图片;“所有项目”;只有一张图片,而不是每个分类法一张。如果有人能帮助我,请。。。花了太多时间,我找不到任何帮助。
这是我的var\\u dump($term),在这3张图片上,只有一张可以显示模式。我想在所有项目上只显示一张图片,当我选择3个过滤器中的一个时,可以单击它并显示模式
SO网友:Tony Djukic
与其获取所有图像ID,然后循环遍历并输出每个ID,不如先将它们收集到一个数组中,然后使用array_unique()
删除重复项。
if( $categories_du_projet ) :
$image_array();
foreach( $categories_du_projet as $term ) :
if( $image_du_projet ) :
$image_array[] = $image_du_projet;
endif;
endforeach;
$image_array = array_unique( $image_array );
foreach( $image_array as $image_article ) :
//now run your output method using $image_article rather than $image_du_projet
//you\'ll probably have to change how you\'re obtaining things like the URL, ALT, etc.
endforeach;
endif;
我不确定的是
$image_du_projet = get_field( \'image_du_projet\' );
而这实际上得到了什么。我假设这是图像的ID。如果这是错误的,你可能需要修改一下。
也许您需要添加以下内容:
$image_du_projet_id = $image_du_projet[\'ID\'];
$image_array[] = $image_du_projet_id;
很抱歉,我不能在这方面给出明确的答案,但我无法真正测试它,因为我没有配置ACF或任何地方的那些字段。