Skip to content

Instantly share code, notes, and snippets.

@mwhiteley16
Created August 26, 2022 12:38
Show Gist options
  • Save mwhiteley16/3052d5b67693a0430b0a732bc8c54818 to your computer and use it in GitHub Desktop.
Save mwhiteley16/3052d5b67693a0430b0a732bc8c54818 to your computer and use it in GitHub Desktop.
ACF block.json script example
(function($){
// front end render on first instance of block only
$(document).ready(function(){
$('.block-faq').each(function(){
faqBlockInit();
return false;
});
});
// back end trigger
if( window.acf ) {
window.acf.addAction( 'render_block_preview/type=wd/acf-faq', acfFaqBlockInit );
}
// set inital block counter
let blockCountFaq = 0;
// back end render for first instance of block only
function acfFaqBlockInit(block) {
blockCountFaq++
if (blockCountFaq > 1) return;
faqBlockInit();
}
// the jQuery
function faqBlockInit() {
$('.block-faq__toggle-question').on( 'click', function() {
$(this).children('.block-faq__toggle-question-icons').attr('aria-selected', function (i, attr) {
return attr == 'true' ? 'false' : 'true'
});
$(this).children('.block-faq__toggle-question-icons').attr('aria-expanded', function (i, attr) {
return attr == 'true' ? 'false' : 'true'
});
$(this).siblings('.block-faq__toggle-answer').slideToggle().attr('aria-expanded', function (i, attr) {
return attr == 'true' ? 'false' : 'true'
});
});
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment