Skip Navigation

[Resolved] Setting a conditional to not display duplicates in a loop

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a View that loops over child posts and outputs each child post's taxonomy terms. I end up with quite a few duplicates I would like to remove from the list of terms.

Solution: In order to break apart the taxonomy terms, you must use spans and separators in the wpv-post-taxonomy shortcode like this:

<wpv-loop>
      <div>
        <h6><span class="genre-string">[wpv-post-taxonomy type="category" separator=", </span><span class='genre-string'>" format="name" order="asc"]</span></h6>
      </div>
</wpv-loop>

Then add some JavaScript to your View's JS panel:

jQuery(document).ready(function(){
  var seenGenre = {};
  // loop over spans
  jQuery('.genre-string').each(function(index,item) {
    var txt = jQuery(item).text();
    var $div = jQuery(item).closest('div');
    // if genre has been seen, remove this span
    if (seenGenre[txt] || seenGenre[txt + ', ']){
        jQuery(item).remove();
    }else{
        seenGenre[txt] = true;
    }
    // now reexamine the parent div and see if it should be removed for lack of content
    if($div.text() == ''){
        $div.remove();
    }
  });
});

Relevant Documentation: https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-taxonomy

This support ticket is created 6 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by taih 6 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#570571

I have a view with a loop: "Narrator Genre-page insert" to display the genres of the author on the Narrators profile page.

The relations are a bit odd, but...
A custom post type "Narrator' is the parent of the "Samples" custom post type. Samples has custom taxonomies of "Genres" and "Dialects".

I have the loop showing all the genres of the child post in a list, but if two samples have the same genre it shows both. I am trying to create a conditional to not show if its already been displayed.

You can see this narrator profile has two entries of "Bio/Memoir, " in the genre loop.
hidden link

#570580

This is the direction I have been trying.

[wpv-conditional if="( '[wpv-taxonomy-slug]' eq 'wpv-taxonomy-slug' )" evaluate="false"]
<h6>[wpv-post-taxonomy type="genre" format="name"]</h6>
[/wpv-conditional]

#570771

Hi, there's not a really easy way to prevent duplicates like this in Views. The conditional you're referring to isn't designed to help you compare a single item with other items. Instead, I could offer a JavaScript solution.

First, you must modify your View so that each genre inside the h6 tag is wrapped in a span tag with a specific class. This will allow us to target each genre individually:

<h6>
  <span class="genre-string">Bio/Memoir, </span>
  <span class="genre-string">Travel/Adventure</span>
</h6>

Then add this JavaScript to your View or Content Template's JS panel

jQuery(document).ready(function(){
  var seenGenre = {};
  jQuery('.genre-string').each(function() {
    var txt = $(this).text();
    if (seenGenre[txt] || seenGenre[txt + ', ')
        jQuery(this).remove();
    else
        seenGenre[txt] = true;
  });
});

I may need to be able to see the code used in your Loop Editor or Content Template to make a more accurate recommendation here.

#570855

Thank you for the direction. I am still a bit stuck unfortunately. I followed the suggestion, but it didn't seem to work.

I think it would have worked, but it has now highlighted a deeper issue causing it.
The genres in this example are taxonomies of samples. They are checkbox fields and when a sample has multiple checkboxes it comes out as "Bio/Memoir, Science" not "Bio/Memoir" and "Science", so the JS isnt catching it.

Bio/Memoir, Science
Bio/Memoir

Is it possible to break those apart by the "," ?

#571061

I see what you mean, let's make some adjustments. In order to break apart the taxonomy terms, you must use spans and separators like this:

<wpv-loop>
      <div>
	    <h6><span class="genre-string">[wpv-post-taxonomy type="category" separator=", </span><span class='genre-string'>" format="name" order="asc"]</span></h6>
      </div>
</wpv-loop>

Here's the updated JavaScript:

jQuery(document).ready(function(){
  var seenGenre = {};
  // loop over spans
  jQuery('.genre-string').each(function(index,item) {
    var txt = jQuery(item).text();
    var $div = jQuery(item).closest('div');
    // if genre has been seen, remove this span
    if (seenGenre[txt] || seenGenre[txt + ', ']){
        jQuery(item).remove();
    }else{
        seenGenre[txt] = true;
    }
    // now reexamine the parent div and see if it should be removed for lack of content
    if($div.text() == ''){
    	$div.remove();
    }
  });
});
#571090

That was absolutely fantastic sir. Thank you for your help.

The forum ‘Types Community Support’ is closed to new topics and replies.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.