Created
January 22, 2025 23:01
-
-
Save VictorPietro/050316bf2f150b3069228bcd6347e8c8 to your computer and use it in GitHub Desktop.
JetSmartFilters Hide widget before filtering (also optionally hide when no filter is applied anymore). All credits to Crocoblock, just saving this for myself. Add the class to the listing grid that you want to hide.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<style> | |
body:not(.elementor-editor-active) .hide-listing { | |
display: none; | |
height: 0; | |
} | |
</style> | |
<script> | |
document.addEventListener( 'jet-smart-filters/inited', init ); | |
function hideProvider( $provider, parentSelector ) { | |
$provider.closest( parentSelector ).addClass( 'hide-listing' ); | |
} | |
function showProvider( $provider, parentSelector ) { | |
$provider.closest( parentSelector ).removeClass( 'hide-listing' ); | |
} | |
function init() { | |
for ( const groupName in window.JetSmartFilters.filterGroups ) { | |
const filterGroup = window.JetSmartFilters.filterGroups[ groupName ], | |
$provider = filterGroup?.$provider, | |
query = filterGroup?.currentQuery || {}; | |
if ( ! $provider.closest('.hide-before-filter')?.length ) { | |
continue; | |
} | |
if ( ! Object.keys( query ).length ) { | |
hideProvider( $provider, '.hide-before-filter' ); | |
} else { | |
showProvider( $provider, '.hide-before-filter' ); | |
} | |
} | |
window.JetSmartFilters.events.subscribe( 'ajaxFilters/updated', function( provider, queryId ) { | |
const filterGroup = window.JetSmartFilters.filterGroups[ provider + '/' + queryId ], | |
$provider = filterGroup.$provider, | |
query = filterGroup.currentQuery || {}; | |
if ( ! Object.keys( query ).length ) { | |
hideProvider( $provider, '.hide-before-filter.hide-on-reset' ); | |
} else { | |
showProvider( $provider, '.hide-before-filter' ); | |
} | |
} ); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment