0

I have a website where I'm showing a list of bookmarks with multiple tags on each one, this is the output:

Bookmark Title
Link
Description text
#tag1 #tag2 #tag3 #tag4 #tag5

Bookmark Title
Link
Description text
#tag3 #tag4

Bookmark Title
Link
Description text
#tag1

(…)

I have a filter where I can select the tags to hide/show the corresponding bookmarks. The problem is now, when there a bookmarks with same tags, the tags also repeating in the filter like:

Filter: #tag1 #tag2 #tag3 #tag4 #tag5 #tag3 #tag4 #tag1

This is my filter:

<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>

How can I delete the duplicates in the filter?

1
  • 1
    so if you have three bookmarks all in say $tag1 you would see $tag1 three times in your filter? Commented Nov 7, 2020 at 15:19

1 Answer 1

1

You need to track the tags you already displayed then conditionally display the tags if they are not already tracked. See below for one way you might do this:

<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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.