Child terms unticked by default

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Child terms unticked by default

This topic contains 15 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 1 year, 9 months ago.

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #38452
    theape
    theape
    Participant

    When a parent category is ticked in a search, the children are ticked too. Is there any way to disable this behaviour, so the parent is ticked, but not the children?

    Image one is what happens by default, image two is the behaviour I would like to happen when the parent is ticked.

    • This topic was modified 1 year, 9 months ago by theape theape.
    Attachments:
    You must be logged in to view attached files.
    #38458
    theape
    theape
    Participant

    To add, I have everything un-checked. The children are ticked when the parent is ticked, I only want the parent ticked when it’s clicked.

    #38471
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    I had to a dig a bit for this, but I recall someone else about a year ago had a similar request, and we found a possible solution with minimal coding.

    Try adding this code to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.

    add_action('wp_footer', 'asp_add_menu_stop_checkbox_parent_child');
    function asp_add_menu_stop_checkbox_parent_child() {
      ?>
      <script>
    	jQuery(function($){
    		$('*[data-lvl]').removeAttr('data-lvl');
    	});
      </script>
      <?php
    }

    This should basically remove the attribute from the checkbox elements, which indicates their levels.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #38472
    theape
    theape
    Participant

    Thank you, it does exactly what I asked for. 🙂

    The problem I have now, is there anyway to make the code more specific to those checkboxes?

    It disables/overrides the “Frontend Search Settings > Categories & Taxonomy Terms > Hide child terms, where the parent checkbox is unchecked?” option. And I’m worried it might have other knock on effects on the website.

    #38473
    theape
    theape
    Participant
    You cannot access this content.
    #38479
    theape
    theape
    Participant

    I’m so close, I don’t know if you can help nudge it over the line.

    	jQuery(function($){
    		$('*[data-lvl]').removeAttr('data-lvl');
    		$(".asp_option_cat_level-1").hide();
    		$(".asp_option_cat_level-2").hide();
    		$(".asp_option_cat_level-0").click(function(){
    			$(this).siblings('.asp_option_cat_level-1').toggle();
    			$(this).siblings('.asp_option_cat_level-2').hide();
    		});
    		$(".asp_option_cat_level-1").click(function(){
    			$(this).siblings('.asp_option_cat_level-2').toggle();
    		});  
    	});

    How do I get this hide/show only the children of the checked parent rather than all of the children when a parent is clicked? I also need to figure out how to have the children automatically unchecked when a parent is unchecked.

    #38481
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    Something like this:

    jQuery(function($){
    	$('*[data-lvl]').removeAttr('data-lvl');
    	$(".asp_option_cat_level-1").hide();
    	$(".asp_option_cat_level-2").hide();
    	$(".asp_option_cat_level-0").click(function(){
    		var $next = $(this).next();
    		console.log(this, $next);
    		while ( $next.hasClass('asp_option_cat_level-1') || $next.hasClass('asp_option_cat_level-2') ) {
    			if ( $next.hasClass('asp_option_cat_level-1') ) {
    				$next.toggle();
    			} else {
    				$next.hide();
    			}
    			$next = $next.next();
    		}
    	});
    	$(".asp_option_cat_level-1").click(function(){
    		var $next = $(this).next();
    		while ( $next.hasClass('asp_option_cat_level-2') ) {
    			$next.toggle();
    			$next = $next.next();
    		}
    	});  
    });
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #38483
    theape
    theape
    Participant

    Thank you, that’s brilliant! The only bit I can’t figure out now is how to get the children to uncheck automatically when the parent is unchecked.

    #38487
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    You are welcome. That should not be too difficult, try this instead of the previous version:

    jQuery(function($){
    	$('*[data-lvl]').removeAttr('data-lvl');
    	$(".asp_option_cat_level-1").hide();
    	$(".asp_option_cat_level-2").hide();
    	$(".asp_option_cat_level-0").click(function(){
    		var $next = $(this).next(), checked = $(this).find('input[type=checkbox]').prop('checked');
    		while ( $next.hasClass('asp_option_cat_level-1') || $next.hasClass('asp_option_cat_level-2') ) {
    			if ( $next.hasClass('asp_option_cat_level-1') ) {
    				$next.toggle();
    			} else {
    				$next.hide();
    			}
    			if ( !checked ) {
    				$next.find('input[type=checkbox]').prop('checked', false);
    			}
    			$next = $next.next();
    		}
    	});
    	$(".asp_option_cat_level-1").click(function(){
    		var $next = $(this).next(), checked = $(this).find('input[type=checkbox]').prop('checked');
    		while ( $next.hasClass('asp_option_cat_level-2') ) {
    			$next.toggle();
    			if ( !checked ) {
    				$next.find('input[type=checkbox]').prop('checked', false);
    			}
    			$next = $next.next();
    		}
    	});  
    });
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #38490
    theape
    theape
    Participant
    You cannot access this content.
    #38491
    theape
    theape
    Participant
    You cannot access this content.
    #38493
    theape
    theape
    Participant
    You cannot access this content.
    #38497
    theape
    theape
    Participant

    For anyone else who runs into this issue, here’s the complete code with the refresh issue solved too.

    add_action('wp_footer', 'asp_add_menu_stop_checkbox_parent_child');
    function asp_add_menu_stop_checkbox_parent_child() {
      ?>
    <script>
    jQuery(document).ready(function($){
    	$(window).load(function(){
    		$('*[data-lvl]').removeAttr('data-lvl');
    		$(".asp_option_cat_level-1").hide();
    		$(".asp_option_cat_level-2").hide();
    		$(".asp_option_cat_level-0").click(function(){
    			var $next = $(this).next(), checked = $(this).find('input[type=checkbox]').prop('checked');
    			while ( $next.hasClass('asp_option_cat_level-1') || $next.hasClass('asp_option_cat_level-2') ) {
    				if ( $next.hasClass('asp_option_cat_level-1') ) {
    					$next.toggle();
    				} else {
    					$next.hide();
    				}
    				if ( !checked ) {
    					$next.find('input[type=checkbox]').prop('checked', false);
    				}
    				$next = $next.next();
    			}
    		});
    		$(".asp_option_cat_level-1").click(function(){
    			var $next = $(this).next(), checked = $(this).find('input[type=checkbox]').prop('checked');
    			while ( $next.hasClass('asp_option_cat_level-2') ) {
    				$next.toggle();
    				if ( !checked ) {
    					$next.find('input[type=checkbox]').prop('checked', false);
    				}
    				$next = $next.next();
    			}
    		}); 
    	});  
    });
    </script>
      <?php
    }
    #38503
    Ernest Marcinko
    Ernest Marcinko
    Keymaster
    You cannot access this content. Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #38505
    theape
    theape
    Participant
    You cannot access this content.
Viewing 15 posts - 1 through 15 (of 16 total)

You must be logged in to reply to this topic.