Skip Navigation

[Gelöst] Remove dash from subcategories in legacy View filter dropdown

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:

How to remove the spaces and dashes 0 in the dropdown filter?

Solution:

Add the JS code below:

jQuery( document ).ready(function() {
  jQuery('select[name="wpv-authors"] option').each(function () {
    var theText = jQuery(this).text();
    theText = theText.replace('  -','');
    jQuery(this).text(theText)
  });
});
jQuery( document ).on( 'js_event_wpv_parametric_search_form_updated', function( event, data ) {
  jQuery('select[name="wpv-authors"] option').each(function () {
    var theText = jQuery(this).text();
    theText = theText.replace('  -','');
    jQuery(this).text(theText)
  });
});

Relevant Documentation:

https://toolset.com/course-lesson/adding-custom-javascript-to-views-templates-and-archives/

This support ticket is created vor 1 Jahr, 10 Monaten. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

Dieses Thema enthält 4 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von sarahP vor 1 Jahr, 10 Monaten.

Assistiert von: Christopher Amirian.

Author
Artikel
#2541577

Hi,
I have created a dropdown filter within a legacy View for a custom taxonomy ("Authors"). I found that subcategories display with a dash in front, and I wish for them to display without a dash.

I found the following support ticket (https://toolset.com/forums/topic/problem-with-input-label-for-child-categories/#post-1488365) with some JS code to solve this, and I have tried to customize it for my View (and pasted it in the JS editor in the Search and Pagination section), but none of my attempts have worked. Should this code still work? If so, can you please look to see what I am doing wrong? Or suggest another method to get the categories to display without the dash?

Thank you!
Sarah

#2541691

Christopher Amirian
Supporter

Sprachen: Englisch (English )

Hi there,

Would you please add the link to the section that has the sub-filters so I can check? Please add it in a way that I can access it without needing to log in.

Thank you.

#2542943
Screenshot 2023-01-27 164023.jpg

Hi, I'm not clear on exactly what link you want...to the page that has the dropdown filter? If so, you can see it here:
versteckter Link
It is the second dropdown for "Authors". As you can see, some of the authors have a dash before their names since they are child terms.

And here is the code I sed for setting up the filters:

[wpv-filter-controls]<div class="content-filter">
	<label for="wpv-category">[wpml-string context="wpv-views"]Filter by Category:[/wpml-string]</label>
	[wpv-control-post-taxonomy taxonomy="category" type="select" output="legacy" url_param="wpv-category"]
	<label for="wpv-authors" style="padding-left:10px;">[wpml-string context="wpv-views"]Authors[/wpml-string]</label>
	[wpv-control-post-taxonomy taxonomy="authors" type="select" url_param="wpv-authors"  output="legacy"]
        [wpv-filter-spinner container="span" spinner="<em><u>versteckter Link</u></em>" style="padding-left:5px"][/wpv-filter-spinner]
</div>
[/wpv-filter-controls]
#2543747

Christopher Amirian
Supporter

Sprachen: Englisch (English )

Hi there,

Please add the JS code below to the view:

jQuery( document ).ready(function() {
  jQuery('select[name="wpv-authors"] option').each(function () {
    var theText = jQuery(this).text();
    theText = theText.replace('  -','');
    theText = theText.replace('  -','');
    theText = theText.replace('-','');
    jQuery(this).text(theText)
  });
});

https://toolset.com/course-lesson/adding-custom-javascript-to-views-templates-and-archives/

Thanks.

#2544561

Thanks, Christopher! Your solution got me about 95% there.

I'll leave an explanation in case this could be of use to someone else in future.

The code you shared did indeed remove the dash, but it did not remove the space, so the child terms still appeared indented.
I found that of all these lines:

theText = theText.replace('  -','');
    theText = theText.replace('  -','');
    theText = theText.replace('-',''); 

strangely, only the last line actually did anything. I tried adding non-breaking space code as used in the original support thread, but that didn't work. I replaced '-' with a copy and paste of the space and dash from the code on the frontend, and for some reason that did the trick.

Next, I discovered that this code only works the first time you choose an item from the filter. Once the term loads, the dashes reappeared. Now, I finally understand the code better from the original support link! So, I used some of the code from there and amended the code you provided also to work when the filter is updated, like so:

jQuery( document ).ready(function() {
  jQuery('select[name="wpv-authors"] option').each(function () {
    var theText = jQuery(this).text();
    theText = theText.replace('  -','');
    jQuery(this).text(theText)
  });
});
jQuery( document ).on( 'js_event_wpv_parametric_search_form_updated', function( event, data ) {
  jQuery('select[name="wpv-authors"] option').each(function () {
    var theText = jQuery(this).text();
    theText = theText.replace('  -','');
    jQuery(this).text(theText)
  });
});

The final issue I had was non-Toolset related...my caching plugin was deferring the loading of JS, so I had to un-defer the loading of the inline JS on the page, and now everything looks good.

I really appreciate you helping me get this fixed!