PHP / 高级自定义字段 (ACF) - 删除重复值
PHP / Advanced Custom Fields (ACF) - Delete duplicate values
我有一个网站,其中显示了一个书签列表,每个书签都有多个标签,这是输出:
Bookmark Title
Link
Description text
#tag1 #tag2 #tag3 #tag4 #tag5
Bookmark Title
Link
Description text
#tag3 #tag4
Bookmark Title
Link
Description text
#tag1
(…)
我有一个过滤器,我可以在其中 select 标签到 hide/show 相应的书签。现在的问题是,当有具有相同标签的书签时,标签也在过滤器中重复,如:
Filter: #tag1 #tag2 #tag3 #tag4 #tag5 #tag3 #tag4 #tag1
这是我的过滤器:
<div class="d-headline--update__filter">
<?php if( have_rows('bookmark', 'option') ):?>
Filter:
<span class="filter__item filter__all active" onclick="filterSelection('all')">Show all</span>
<?php while( have_rows('bookmark', 'option') ) : the_row();?>
<?php $bookmarkTags = get_sub_field('bookmark_tags', 'option');
if( $bookmarkTags ): ?>
<?php foreach( $bookmarkTags as $bookmarkTag ): ?>
<span onclick="filterSelection('<?php echo $bookmarkTag; ?>')" class="filter__item filter__<?php echo $bookmarkTag; ?>"><?php echo $bookmarkTag; ?></span>
<?php endforeach; ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
如何删除过滤器中的重复项?
您需要跟踪已显示的标签,然后有条件地显示尚未跟踪的标签。请参阅下面的一种方法:
<div class="d-headline--update__filter">
<?php if( have_rows('bookmark', 'option') ):?>
Filter:
<span class="filter__item filter__all active" onclick="filterSelection('all')">
Show all
</span>
<?php
// collection of tags for this page
$bookmarkTagsCollection = [];
while( have_rows('bookmark', 'option') ) : the_row();?>
<?php $bookmarkTags = get_sub_field('bookmark_tags', 'option'); ?>
<?php if ( $bookmarkTags ) : ?>
<?php foreach( $bookmarkTags as $bookmarkTag ): ?>
<?php // only display if this tag has not been displayed yet ?>
<?php if (!in_array($bookmarkTag, $bookmarkTagsCollection)) : ?>
<span onclick="filterSelection('<?php echo $bookmarkTag; ?>')" class="filter__item filter__<?php echo $bookmarkTag; ?>">
<?php echo $bookmarkTag; ?>
</span>
<?php
// add current tag to collection to it does not get displayed again if later posts also contain this tag
$bookmarkTagsCollection[] = $bookmarkTag;
?>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
我有一个网站,其中显示了一个书签列表,每个书签都有多个标签,这是输出:
Bookmark Title
Link
Description text
#tag1 #tag2 #tag3 #tag4 #tag5
Bookmark Title
Link
Description text
#tag3 #tag4
Bookmark Title
Link
Description text
#tag1
(…)
我有一个过滤器,我可以在其中 select 标签到 hide/show 相应的书签。现在的问题是,当有具有相同标签的书签时,标签也在过滤器中重复,如:
Filter: #tag1 #tag2 #tag3 #tag4 #tag5 #tag3 #tag4 #tag1
这是我的过滤器:
<div class="d-headline--update__filter">
<?php if( have_rows('bookmark', 'option') ):?>
Filter:
<span class="filter__item filter__all active" onclick="filterSelection('all')">Show all</span>
<?php while( have_rows('bookmark', 'option') ) : the_row();?>
<?php $bookmarkTags = get_sub_field('bookmark_tags', 'option');
if( $bookmarkTags ): ?>
<?php foreach( $bookmarkTags as $bookmarkTag ): ?>
<span onclick="filterSelection('<?php echo $bookmarkTag; ?>')" class="filter__item filter__<?php echo $bookmarkTag; ?>"><?php echo $bookmarkTag; ?></span>
<?php endforeach; ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
如何删除过滤器中的重复项?
您需要跟踪已显示的标签,然后有条件地显示尚未跟踪的标签。请参阅下面的一种方法:
<div class="d-headline--update__filter">
<?php if( have_rows('bookmark', 'option') ):?>
Filter:
<span class="filter__item filter__all active" onclick="filterSelection('all')">
Show all
</span>
<?php
// collection of tags for this page
$bookmarkTagsCollection = [];
while( have_rows('bookmark', 'option') ) : the_row();?>
<?php $bookmarkTags = get_sub_field('bookmark_tags', 'option'); ?>
<?php if ( $bookmarkTags ) : ?>
<?php foreach( $bookmarkTags as $bookmarkTag ): ?>
<?php // only display if this tag has not been displayed yet ?>
<?php if (!in_array($bookmarkTag, $bookmarkTagsCollection)) : ?>
<span onclick="filterSelection('<?php echo $bookmarkTag; ?>')" class="filter__item filter__<?php echo $bookmarkTag; ?>">
<?php echo $bookmarkTag; ?>
</span>
<?php
// add current tag to collection to it does not get displayed again if later posts also contain this tag
$bookmarkTagsCollection[] = $bookmarkTag;
?>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
</div>